I'll like to format 1000 to 10.00
The PHP number_format function does not seem to be working for this.
I have tried:
$amount2 = number_format("$cost",2,"",",");
echo "$cost";
Any ideas? Is there a way I can manupulate number_format to display the results (i.e just inserting a decimal before the last two digits?
Number format will change the "." to a "," but you telling it to format ONE THOUSAND.
$cost=1000;
echo number_format($cost,2,'.',',');
//1,000.00
What you want is simply:
$cost=1000;
echo number_format($cost/100,2,'.',',');
//10.00
Is this legit for you ?
<?php
$cost=1000;
echo substr($cost, 0, 2) . "." . substr($cost, 2);//10.00
1000 and 10.00 are totally different numbers (in values). Divide by 100, then format it properly:
$cost = 1000 ;
$cost /= 100 ;
$amount2 = number_format($cost,2,".","");
echo $amount2 ;
Try this code:
$stringA= 1000;
$length=strlen($stringA);
$temp1=substr($stringA,0,$length-2);
$temp2=substr($stringA,$length-2,$length);
echo $temp1.".".$temp2; // Displays 10.00
The third parameter to number_format should be the character you want to use as a decimal point. Why are you passing an empty string? And why are you placing your number ($cost) inside a string?
Try this: echo number_format($cost,2,'.',',');
EDIT: Perhaps I misunderstood your question — if you want the number 1000 to be displayed as 10.00, just divide $cost by 100 before calling number_format().
Related
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 want to format numbers like following
13.20 to 13.2
13.34 to 13.34
13.00 to 13
I have tried to use a combination of str_replace() and number_format() but not able to produce required result.
Please help me, if anyone have any idea.
Thanks in advance
Try using the built-in round function:
echo round(1.23456, 2);
This will return 1.23. Of course, you have to decide how many numbers after the decimal point to keep.
number_format should work:
<?php
$number = 12.345;
echo number_format($number, 1); # Produces number with one decimal precision.
?>
EDIT:
<?php
$number = 12.30;
#Strips ZEROs and decimal point from the end
echo rtrim($number, '0.'); #Result: 12.3
?>
Or just simply, this is magic :
$number + 0
// 12.30 + 0 = 12.3
Or you can cast your number to float :
echo floatval($number);
It depends on the type of variable you're using. A string 13.20 will output 13.20 but a float 13.20 will output 13.2.
All you have to do is cast it:
echo (float) '13.20'; // Will output 13.2
Have such mysql query SELECT CurrencyRate/Units AS FinalCurrencyRate
Value for CurrencyRate is 0.06200000 and value for Units is 1000.
So 0.06200000 / 1000 and get 6.2E-5
The same result if echo $result = 0.06200000 / 1000 . '<br>';
If echo $result = number_format( (0.06200000 / 1000), 10, '.', '' ) . '<br>'; then can get 0.0000620000
What is solution for mysql query to get normal number instead of 6.2E-5?
Found round(CurrencyRate/Units,10)
But what if do not know number of 00000 after decimal? For example 0.06200000 / 1000000000000000000
use type DECIMAL for such values. This would make your values display the way you mentioned.
Because you are working with currencies, use BC Math Functions for better percision. You can set the number of digits after the decimal place in the result when you use them. Example:
//bcdiv - it divides to numbers
echo bcdiv('105', '6.55957', 3); // 16.007
A log I've built isn't getting correct numbers reported...
0.01% - 1,362 (incorrect %)
9.13% - 814 (correct)
0.66% - 59 (correct)
Here is my code...
$count2 = mysql_num_rows($result2);
$n = 100/$count2*number_format($value);
echo round($n,2).'%';
number_format is going to add commas and such and that will mess up round as it doesn't expect strings. Try using number_format after using round like this:
$count2 = mysql_num_rows($result2);
$n = 100/$count2*$value;
echo number_format(round($n,2)).'%';
'Number Format' is adding a comma in values over a thousand therefor multiplying by a string.
Removing number_format() from both lines corrected the problem.
How to make number like 3.0000000054978E+38 to 3.00 in PHP?
Many thanks!
You cannot use round to solve this since it is a number in scientific notation. You can, however, use substr:
$i = 3.0000000054978E+38;
$i = substr($i, 0, 2); // $i is now the string 3.00
echo( number_format($i+1,2) ); // Will output 4.00
In case you are looking for the small fraction of your number being outputted in a formatted fashion:
$number = 3.0000000054978E+38;
printf('%.2f', $number / 1E+38); # 3.00
You can just use round, as in round($floating_number, 2).
sprintf() always gives you the specified number of decimal points, if you require.
sprintf('%0.2f', 3.0000000);
Would display 3.00, if you echo it.