This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 8 years ago.
Having the following decimal numbers:
47.44
180.11
340
12.39
25
I was wondering, how can I add a default .00 to those numbers, that have no decimals?
You can use number_format() like that:
number_format($number, 2);
It'll always return number with two decimals. Also it'll separate thousands with ',' (if you don't want that, add '' as fourth parameter).
Related
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 2 years ago.
i need to round off up-to 2 decimal,currently i getting 12% but the actual discount is 11.76
my code as follow
$perc=0;
if($value->IsOfferItem){
$perc=round(100-(($value->SellingPrice/$value->ActualPrice)*100));
}
Here's an example
$table->decimal('amount', 5, 2);
In above example first parameter is the field name. Second, parameter is the total length. Third, parameter is the float value.
the table field, for example in mysql must contain decimal (10.2), example:
amount dcecimal(10,2) not null
The 2 indicates decimal places.
The 10 indicates the maximum numerical quantity before the comma.
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 5 years ago.
I'm currently working on retrieving data from coinmarketcap API and I would like to limit the strings to 5. I retrieve the data and then multiply the value *1.07 and the result to MXN, but the string is long, like 25.65675734343, I want to limit that string, this is my echo:
echo $xrpprice*$rate*$fxrates['rates']['MXN'];
Use round:
$result = $xrpprice*$rate*$fxrates['rates']['MXN'];
echo round($result,2);
In PHP you can use number_format to control how many decimal places. Assuming you don't want to limit the whole number to just five characters.
echo number_format($numberToFormat, $numberOfDecimalPlaces);
Please better explain your question. Also, what have you tried?
This question already has answers here:
How to strip trailing zeros in PHP
(15 answers)
Show a number to two decimal places
(25 answers)
Closed 7 years ago.
This questions is kinda confusing, but here I am telling you the exact problem I have.
In database I have a column with datatype double(10,6), means the value can have upto 6 decimals depending on each user.
Some user have a value 5.22 say in this column.
Now when I echo this value in php, I want it to show 5.22 or 7.256 not 5.220000 or 7.256000.
I hope its clear.
Thanks.
This question already has answers here:
PHP float/double stored as MySQL DECIMAL
(2 answers)
Closed 8 years ago.
I have a table with a DECIMAL(10,6) column. I have a string representation of a float (e.g. 35.3123122). I am using php to insert the value. I used floatval($string) to insert and it did not work. I also tried to insert the string itself with no success. Do I need to format the float value exactly to match the 10,6 criteria. If so, How can I do this?
Thanks
Not sure if this is your problem, but the value of "35.3123122" has 7 digits after the decimal point. DECIMAL(10,6) is 10 digits in total with a maximum of 6 digits past the decimal point.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Round up to the nearest 50,000 in PHP
I have this, 52.52 and I want it to become 52.5. How would I do that in PHP? I tried round() but did not work. Thanks.
round($num, 1);
should round $num to the nearest tenth (the second argument specifies the precision, or the number of digits after the decimal it should round to)
Try
$x = number_format('52.52', 1)
Documentation is here