This question already has answers here:
Remove useless zero digits from decimals in PHP
(29 answers)
Closed 6 years ago.
javascript Number function is handy when dropping undesired zero if decimal part is zero e.g
Number(2.00)
2
Is there such function in php or any alternative
I think this works that way by default in PHP. If you use proper number type like float or double.
If you're using string then you need to map
$a = '2.00';
echo (float)$a; // 2
Example of using float
$a = 2.00;
echo $a; //2
or
2.00 + 0; //2
If you want to format the number to show decimal part 2.00 you need to use number_format function (http://php.net/manual/en/function.number-format.php)
$a = 2.00
echo number_format($a, 2); // 2.00
You can achieve this by adding zero ... for example
echo "Result " . (2.00 + 0) . "\n";
Result 2
you can use floatval()
echo floatval('2.00'); //2
echo floatval('2.23'); //2.23
See the documentation for more info
you can use intval() or any roundoff function
$_float = 1.9020441;
$_float = explode(".", $_float);
echo strlen($_float[1]);`
Related
This question already has answers here:
How can I format a number into a currency value without php?
(3 answers)
Closed 4 years ago.
I am calculating result of my expression using eval() function but its showing all the numbers afer decimal and I want to show only two numbers after decimal
this is my eval function with other data that I have stored.
$expression = generate_expression($num_operands,
$operations, $num_digits);
$expression_string = implode(" ", $expression);
$result = eval("return ($expression_string);");
$expressions[] = compact("expression", "result");
I am storing the expression_
for this I tried using round function also but its not showing the result that I want
here is the round function for the $result
<?php echo round($result,2); ?>
Where I went wrong?
Use: number_format()
<?php echo number_format((float)$result, 2); ?>
Example
This question already has answers here:
Delete digits after two decimal points, without rounding the value
(15 answers)
Closed 8 years ago.
I have a value 3.9609053497942. I need value 3.9 means only one value after decimal. I have used PHP number_format and round functions but it is giving me answer 4.
You could multiply the number by 10, floor() it, and then divide it back.
echo floor($value * 10) / 10;
Try with this,
echo intval((3.9609053497942*10))/10;
or
echo floor((3.9609053497942*10))/10;
There is so many possible solutions:
echo bcadd(3.9609053497942, 0, 1);
preg_match('/\d*\.\d/', 3.9609053497942, $matches);
echo $matches[0];
why not treat it as a string, like
$x = (string)3.96;
$y = explode(".",$x);
$result = $y[0] . "." . $y[1];
Did you tried like:
number_format(3.9609053497942, 1);
This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
hello i'm trying to find %. first i found the seconds
$tm=sum_the_time($d_duration);
$d_seconds='0';
list($hour,$minute,$second) = explode(':', $tm);
$d_seconds += $hour*3600;
$d_seconds += $minute*60;
$d_seconds += $second;
$total_second=$c_seconds+$p_seconds+$t_seconds+$b_seconds+$d_seconds;
$c_seconds=$c_seconds*100/$total_second;
$p_seconds=$p_seconds*100/$total_second;
$t_seconds=$t_seconds*100/$total_second;
$b_seconds=$b_seconds*100/$total_second;
$d_seconds=$d_seconds*100/$total_second;
echo $c_seconds;
the result is 10.754504504505, how would I print this code like 10.7
You can try using printf() function:
printf("%.1f", $c_seconds);
Or, number_format():
echo number_format( $c_seconds, 1 );
These two functions will round your number (will return 10.8 in your example), so, if you want to just truncate to the first decimal place (result to be equal to 10.7), you can use the following:
echo substr($c_seconds, 0, strpos($c_seconds, ".") + 2);
Actually, you can use the solutions from this question to better use number_format() and get your desired result.
echo sprintf('%0.1f', $c_seconds);
relevant docs here: http://php.net/sprintf
http://php.net/manual/en/function.number-format.php numberformat is propably what you are looking for.
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);