This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 11 months ago.
I need to convert numbers to have .00 after them, but only if the number is an integer, or it has just 1 number after the decimal point, like so:
1.4 = 1.40
45 = 45.00
34.77 = 34.77
What reg exp to use for this simple case?
You can also use printf or sprintf
printf("%01.2f", '34.77');
$formatted_num = sprintf("%01.2f", '34.77');
number_format($number, 2, '.', '');
Read more at PHP.net. You don't need to determine if a number is an integer or not -- as long as it's a number, it will be formatted to two decimal places.
If you'd like the thousands separator, change the last parameter to ','.
Check out PHP's built-in function number_format
You can pass it a variable and it'll format it to the correct decimal places
$number = 20;
if (is_int($number)) {
$number = number_format($number, 2, '.', '');
}
read number_format
number_format($number, 2, '.', '');
$num = 0.00638835;
$avg = sscanf($num,"%f")[0] /100;
echo sprintf("%.10f", $avg);
result 0.0000638835
Related
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 5 years ago.
I have a value in database which contains 20 numbers, example :
1.1234567891011223
I want to limit the echoed value to 1.123 or 1.12 only instead of this long number..
How can I do that in PHP?
It sounds like you're looking for either the round function:
$long_number = 1.1234567891011223;
$formatted_number = round($long_number, 2);
echo $formatted_number; // 1.12
Or the number_format function:
$long_number = 1.1234567891011223;
$formatted_number = number_format($long_number, 2, '.', '');
echo $formatted_number; // 1.12
Hope this helps! :)
use round function
echo round(1.1234567891011223,2); // output: 1.12
is this what you are searching for? http://php.net/manual/de/function.number-format.php
use it like that:
<?php
$a = 1.1234567891011223;
echo number_format ( $a , 3 , '.', '');
?>
Take a look at strpos and substr. Below is an example from another post #Nineoclick This function finds the decimal and rounds the number. Rounding the number will shorten it.
truncate(1.1234567891011223, 2)
function truncate($val,$p = 0)
{
$strpos = strpos($val, '.');
return $p < 0 ? round($val,$p) : ($strpos ? (float)substr($val,0,$strpos+1+$p) : $val);
}
You can also use sprintf for this
$formatted = sprintf("%01.2f", $longNum);
Or if you're just going to echo it and don't need the variable, printf
printf("%01.2f", $longNum);
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 7 years ago.
I have a variable
$amount = 621.00;
I need the 30% of the variable with output 186.30
When I calculate:
$amount2 = round($amount*30/100 , 2);
echo $amount2
gives the output of 186.3
how can I have output 186.30
number_format() should do the trick.
// english notation without thousands separator
$amount2 = number_format($amount2, 2, '.', '');
// 1234.57
You could use number_format for that:
$amount2 = number_format($amount * 30 / 100, 2);
echo $amount2;
You can use number_format
For example number_format($amount2, 2)
More details: http://php.net/manual/en/function.number-format.php
use number_format($amount2,2) at the end.
See documentation here: http://php.net/manual/en/function.number-format.php
This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
I have a DOUBLE data type column in my MySQL table and I have a value of:
14.5299
But PHP number_format rounds that value to:
14.5300
And if I just echo 14.5299, it outputs:
14.53
How can I output 14.5299 without hardcoding anything in my HTML markup?
Use sprintf()
echo sprintf("%2.4f", 14.5399);
Documentation: http://us2.php.net/sprintf
This worked for me:
<?php
$double = 14.5299;
echo number_format(floor($double*100)/100, 2);
?>
Result is: 14.52
$double = 14.5299;
var_dump( number_format($double, 4, '.', ',') );
... prints:
string(7) "14.5299"
... as expected.
My educated guess is that your number has actually more 9's:
$double = 14.52999;
... in this case, 14.5300 is the correct way to round.
The next function should works:
function fixedNumberFormat($float, $decimals, $withFormat = true) {
$decPoint = ($withFormat) ? "," : null;
$thousandsSep = ($withFormat) ? "," : null;
return substr(number_format($float, $decimals + 1), 0, -1);
}
fixedNumberFormat(14.5299, 4);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: show a number to 2 decimal places
How can I format an input number to be 0.00 if it has not any value? I tried (double) but it prints 0 only.
Here you go :)
echo number_format($var,2);
If you want it to print specific no. of decimal points, use number_format.
$float_var = number_format($var, 2);
$var = number_format($number, 2, '.', '');
This forces 2 points after the decimal, sets the decimal as a period. You can also forego the last two as it defaults to it;
Note: The third value is your decimal separator, the fourth value is the thousandths separator.
$var = number_format($number, 2);
For direct output:
printf('%0.2f',$var);
Output into variable:
$outVar = sprintf('%0.2f',$var);
This statemant casts $var type to float and prints with 2 decimal signs
maybe you should check it first if the value is not set
if(!isset($variableName))
{
// then set
$variableName = "0.00"; // => string
//or like this
$variableName = number_format(0,2); // => this result is also string
}
echo "value: ",$variableName;
result
0.00
if you are trying to format the value i sugest you to use meioMask pluging.
So you define your field as number and the pluging do the trick, even if you set "0" for the value
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Default Number of Decimal Places to Output in PHP
basically a bit of a maths problem,
$average_ppm = $total_points_given / $totalvalue;
$average_ppm now equals 2.432608695652174, I don't want to display these numbers, I just need $average_ppm to be 2.43, so to a fixed 2 decimal points. How can I do this??
Thanks for anyones time.
Use sprintf if you want a string output, or round/floor/ceil for a numeric value:
$average_ppm = 2.432608695652174;
echo sprintf("%.2f", $average_ppm); // 2.43
$approx_average_ppm = round($average_ppm, 2);
echo $approx_average_ppm; // 2.43
echo floor($average_ppm, 2); // 2.43 , even if $average_ppm = 2.439
echo ceil($average_ppm, 2); // 2.44
You could either use sprintf, round or floor/ceil depending on how you want the numbers rounded.
Most suited for your need would be round:
$average_ppm = round($total_points_given / $totalvalue,2);
If you want to have ALWAYS 2 numbers after... you can do it with number_format:
number_format(2.43260869565217, 2); // 2.43
When you got a number like: 2.400054846 and you use round you will get 2.4
and if you want it with 2 number behind you can use number_format this will output 2.40
I would use Round:
round($average_ppm, 2);
You could use the bcmath functions if they are available where the third argument is the precision
$average_ppm = bcdiv($total_points_given, $totalvalue, 2);