the code below is supposed to format the number i am getting after running multiplication in mysql to get data in a temporary table. problem is the result of the multiplication is not following the formatting. I want the number formatted to have four decimal places and the zeros retained.
<?php
$tv = $row_cscs_report['TOTAL_VALUE'];
$vr = str_replace('.', '', $tv);
echo $vr;
?>
You can do this using number_format, specifying 4 decimal places and telling it that the decimal and thousands separators should both be empty:
$tv = 3547.66;
echo number_format($tv, 4, '', '');
Output:
35476600
you can write like this:
<?php
$tv = $row_cscs_report['TOTAL_VALUE'];
$vr = sprintf("%.4f", $tv);
echo $vr;
?>
Reference:
http://php.net/manual/pl/function.sprintf.php
Related
At the the moment, I stored values in my mysql database as a decimal value (199,54).
But If I get this value with php (mysql query) and would like to calculate with it:
echo ($row->myValue) / 5;
I get the error:
A non well formed numeric value encountered ...
Where is my mistake?
You can use floatval function to convert decimal value that is fetched from database and then you can use in calculation:
$floatValue = floatval($row->myValue);
Now you can use $floatValue variable in any calculation.
As others have pointed out decimal separator in php is dot (.) not comma (,).
Either change the separator on the DB level to . or run the following anytime you want to deal with numbers stored in the database. First two lines are just an example to get it running locally for me
<?php
$row = new stdClass();
$row->myValue = "15,29";
$myValue = str_replace(',', '.', $row->myValue) / 5;
echo $myValue;
?>
I am trying to create an ecommerce store and our prices need to fluctuate with the exchange rate for different countries so I'm dealing with a lot of decimal places.
What I want to do is round the original price to the nearest full number (as in they can keep the change). But then I want to format that as a currency with two decimal places.
<?php
$number = 12345.6789;
echo $number; // outputs '12345.6789'
$number = number_format($number,0);
echo $number; // outputs '12,346'
$number = number_format($number,2);
echo $number; // outputs '12.00'
?>
After formatting to no decimal places it starts reading the ',' as the decimal separator instead of the thousands separator and formats that for two decimal places.
It also gives the following error:
A non well formed numeric value encountered in C:\wamp64\www\Lifting365\test.php on line 6
How can I achieve what I am looking for?
As specified in the documentation, number_format returns a string value, you can't reuse it as a number.
Use the function round() to round your number, if you want to round it to the direct upper integer use ceil() instead.
number_format(round(12345.6789), 2);
// apply intval to get the low integer value (for change purposes)
$number = 12345.6789;
echo $number; // outputs '12345.6789'
echo intval($number)."<br/>"; // outputs '12345'
echo number_format(intval($number),0,'.','.'); // outputs '12.345'
echo number_format(intval($number),0,'.',','); // outputs '12,345'
Use round function and then number_format.
// returns 12,346.00
number_format(round(12345.6789), 2);
The function number_format accepts 4 parameters. Per default a point will be used as decimal seperator and comma as thousands seperator (12345.6789 become 12,346 after your first call; as excepted). It's not explicitly documented but number_format also rounds.
http://php.net/manual/de/function.number-format.php
string number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," )
You are getting an error because you reuse the same variable $number. After your first call to number_format you dont have a float value anymore.
<?php
$number = 12345.6789;
echo $number."<br>"; // outputs 12345.6789
echo number_format($number,0)."<br>"; // outputs 12,346
echo number_format($number,2)."<br>"; // outputs 12,345.68
?>
If you are not sure what is in your variable you can apply floatval to it.
echo number_format(floatval($number),2);
The PHP function that you're looking for is money_format() http://php.net/manual/en/function.money-format.php have a good read through the manual page (including the comments)
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
?>
I have been trying all morning but how would I this number 1304583496 to look like this
13.04583496
and the same goes for this number
456604223 to 4.56604223
There are always 8 numbers to the right.
Divide it with 100000000, or use "pow" function:
$number / pow(10, 8);
You could also use the official number_format method after division to keep your ending zeroes and have your number displayed in a nice manner.
<?php
$num = 1304583496; //the number
echo number_format($num/100000000,8,"."," "); //number of decimals = 8, comma seperator is . and thousands seperator is a space here
?>
For more information on this function: http://www.w3schools.com/php/func_string_number_format.asp
I am getting the following output correctly:
<?php echo number_format("12312.312","1"); // Correct Output 12312.3 ?>
but in the following case
<?php echo number_format("12312","1"); // Getting output 12312.0 but requires only 12312 ?>
So basically, I want to control my output i.e. it should add decimal place only if my decimal digit is greater than 0.
The second parameter for number_format() takes the number of decimals - so your example is the the expected result. Maybe you are intereseted in the round() function which allows to round to a certain precision?
You can try something like this
<?php
$number = 12312;
echo is_int($number) ? $number : number_format($number,"1");
?>
If you don't want the extra decimal place, use <?php echo number_format(12312, 0);?>
The "0" represents 0 decimal places
simple casting it to float, usign (float), works.
e.g.
$num_1 = (float)1.0;
$num_2 = (float)1.1;
echo $num_1;
echo $num_2;
output:
1
1.1