I am extremely new to PHP, and I am having some issues with the number_format() function.
I am performing a calculation which is, correctly, returning this result: 6215.
However, I want this value to be displayed/echoed as 62.15. I have played around with the number_format() function to no avail.
Any help would be greatly appreciated!
You have 2 potential tasks here.
1.) You want to change your number (divide by 100)
<?php
$number = 6215;
$number = $number / 100;
echo $number;
?>
Renders as 62.15
2.) You may want additional formatting to make a "pretty" number
From the docs: https://www.php.net/manual/en/function.number-format.php
// Function signature syntax
number_format(
float $num,
int $decimals = 0,
?string $decimal_separator = ".",
?string $thousands_separator = ","
)
<?php
$number = 1006215.56;
$pretty_number = number_format($number, 2, '.', '');
echo $pretty_number;
?>
Renders as 1,006,215.56
This was directly pulled from the php.net documentation, which I think is really great.
https://www.php.net/manual/en/function.number-format.php
<?php
$number = 1234.56;
// english notation (default)
$english_format_number = number_format($number);
// 1,235
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56
$number = 1234.5678;
// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
?>
Related
I am having a problem with php conversion from string to float
In some cases, if I do:
floatval("8.80")
I get:
8.800000000000001
I have struggled with round(x,1), number_format, etc, but to no avail
What am I getting wrong here?
By using number_format will help to resolve your issue
<?php
$number = 8.800000000000001;
$precision = 1;
$number = intval($number * ($p = pow(10, $precision))) / $p;
echo number_format((float) $number, $precision, '.', '');
?>
I'm trying to format an integer with 5 digits using php with number format plus I'm not getting the result waiting.
Example: 35914
Expected result: 35.91
Note that I pretending limits to only 2 decimal places after the point.
My attempts:
<?php
$number = 35914;
echo $english_format_number = number_format($number)."<p>";
// 35,914
// Notação Francesa
echo $nombre_format_francais = number_format($number, 2, ',', ' ')."<p>";
// 35 914,00
// Notação Inglesa com separador de milhar
echo $english_format_number = number_format($number, 2, '.', '')."<p>";
// 35914.00
?>
I do not get exactly your problem, but I would recommend to have a look on String functions (http://php.net/manual/en/ref.strings.php).
Using the str_split function, for example, we can transform the input number in the expected output.
<?php
$input = 35914;
$editedNumber = str_split($input,2);
$editedNumber = $editedNumber[0] . "." . $editedNumber[1];
echo $editedNumber; // 35.91
?>
Let's say I have numbers like
123456000000
12345000000
123456000
123456
and I want them to show up as
123.45B
12.345B
123.45M
123,456
The best way I can think of to do this is by getting string length to determine if I need a B, M, or nothing but commas and just substr the first five chars. I'm sure there is a better way though and I don't want to get too far before I realize that my idea sucks. hah.
Any good recommendations?
EDIT
My apologies on the confusion of the B and M. Those represent:
B illion
M illion
Okay, so my previous answer considered you were dealing with file sizes, and I deleted it. However the logic of this solution is the same:
function format_number($num) {
if($num < 1000000) {
return number_format($num);
} elseif ($num < 1000000000) {
return number_format($num/1000000, 2) . 'M';
} else {
return number_format($num/1000000000, 2) . 'B';
}
}
http://codepad.org/nb89ze5J
Don't know about the last B or M part but there is a function called number_format in PHP which does the following (from the documentation) -
$number = 1234.56;
// english notation (default)
$english_format_number = number_format($number);
// 1,235
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56
$number = 1234.5678;
// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
I have a basic index.php page with some variables that I want to print in several places - here are the variables:
<?php
$firstprice = 1.50;
$secondprice = 3.50;
$thirdprice = 20;
?>
My challenge is that later in the document, when I print, I get the prices without the second '0' in the price - this is what happens:
<?php print "$firstprice";?> // returns 1.5 - not 1.50!
SO - I know how to do this with JS, but how is this done in PHP 5+? Basically I want to print the second '0' if there is already a decimal, so if the variable is equal to '3', it stays as '3', but if it's equal to '3.5' it converts to display '3.50' with a second '0', etc.
Here's a JS example - what's the PHP equivalent?
JS:
.toFixed(2).replace(/[.,]00$/, ""))
Many thanks!!
This is simple and it will also let you tweak the format to taste:
$var = sprintf($var == intval($var) ? "%d" : "%.2f", $var);
It will format the variable as an integer (%d) if it has no decimals, and with exactly two decimal digits (%.2f) if it has a decimal part.
See it in action.
Update: As Archimedix points out, this will result in displaying 3.00 if the input value is in the range (2.995, 3.005). Here's an improved check that fixes this:
$var = sprintf(round($var, 2) == intval($var) ? "%d" : "%.2f", $var);
<?php
$number = 1234.56;
// english notation (default)
$english_format_number = number_format($number);
// 1,235
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56
$number = 1234.5678;
// english notation without thousands seperator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
?>
more info here
http://php.net/manual/en/function.number-format.php
You could use
if (is_float($var))
{
echo number_format($var,2,'.','');
}
else
{
echo $var;
}
What about something like this :
$value = 15.2; // The value you want to print
$has_decimal = $value != intval($value);
if ($has_decimal) {
echo number_format($value, 2);
}
else {
echo $value;
}
Notes :
You can use number_format() to format value to two decimals
And if the value is an integer, just display it.
you can use number_format():
echo number_format($firstprice, 2, ',', '.');
Alternatively way to print
$number = sprintf('%0.2f', $numbers);
// 520.89898989 -> 520.89
I want that real numbers would be for example 12.92, but not 12.9241. Is it possible to do like that?
In PHP, try number_format:
$n = 1234.5678;
// Two decimal places, using '.' for the decimal separator
// and ',' for the thousands separator.
$formatted = number_format($n, 2, '.', ',');
// 1,234.57
For PHP you can use number_format(), for MySQL use the FORMAT() function.
MySQL: http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_format
FORMAT(number, 2)
Example:
mysql> SELECT FORMAT(12332.123456, 4);
-> '12,332.1235
PHP: http://php.net/manual/en/function.number-format.php
$number = 1234.5678;
$formatted_number = number_format($number, 2, '.', '');
// 1234.56
$number = 1234.5678;
$teX = explode('.', $number);
if(isset($teX[1])){
$de = substr($teX[1], 0, 2);
$final = $teX[0].'.'.$de;
$final = (float) $final;
}else{
$final = $number;
}
final will be 1234.56
You can multiply your number by 100, do a rounding of the result and then divide back by 100.
Or in php use the round function round function
$result=round(12.9241, 2);