This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to display number in a certain format in php
I have following digit : 3234.54554545545
I want to split it like 3234.54, means I want to display only 2 char after dot.
Following is something I am trying but its not working
<?php
$v = "3234.54554545";
$pos = strpos($v, '.');
$r = substr($v, count($pos) - 3);
echo $v."".$r;
?>
Actual result: 3234.54554545545
Desired output : 3234.54
Please help me. Thanks
You can use number_format function as:
$v = "3234.54554545";
$r = number_format($v, 2, '.', '');
Why can't you use number_format?
<?php
$v = "3234.54554545";
$r = number_format($v, 2);
?>
PHP is super-flexible with it's types, and numeric strings can be used directly as numbers.
//This is true
('123.45' == 123.45)
As such you can use number_format to format the string. You can also use sprintf if you need more control.
Related
This question already has answers here:
Add zero to before decimal point of a number
(2 answers)
Closed 2 years ago.
I have some data from database like this :
43966.31875
how to change 43966 to 0 and keep decimal number to 31875, What should I do?
well, for the final result like this :
0.31875
If my explanation is incomprehensible, I apologize, and you can ask me again, Thank You
The regex method:
$string = '43966.31875';
echo preg_replace('/(\d+).(\d+)/i', '0.${2}', $string);
Will split number into 2 parts then replace the part 1 with "0."
The strstr method:
$string = '43966.31875';
echo '0'.strstr($string, '.');
Also split the number into 2 parts and take the second part and add "0" before
$number = 43966.31875;
$decimalPart = fmod($number, 1);
$decimalCount = explode('.', $number)[1];
$result = round($decimalPart, strlen($decimalCount));
Refer the Official Documentation:
PHP fmod
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:
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:
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.
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);