Round to the nearest tenth? [duplicate] - php

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

Related

PHP round three decimal places up with problem [duplicate]

This question already has answers here:
Rounding up to the second decimal place [duplicate]
(2 answers)
PHP: Round a number upto 2 decimal places and if the number is a whole number then add trailing zeros
(4 answers)
Closed 2 years ago.
Thank you if you help me to solve this.
I need this round up with 2 decimal
<? echo round(ceil((0.55*100))/100,2); // output 0.56 ?>
I want this
0.050 -> 0.05
0.051 -> 0.06
0.055 -> 0.06
0.059 -> 0.06
0.060 -> 0.06
Thank you!

how to round to decimal point in laravel [duplicate]

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.

Limit echo string number [duplicate]

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?

Can you safely compare two floats by rounding them in PHP? [duplicate]

This question already has answers here:
How should I do floating point comparison?
(12 answers)
Closed 7 years ago.
Rather than using an epsilon for float comparison, can you reliably compare two floats for equivalency by rounding them to the desired precision?
For example:
round($float, 3) === round($otherFloat, 3)
No. If your numbers are just barely on opposite sides of the value where the function will round up instead of down (a half-integer if you're round to the nearest integer), then they will round to different numbers no matter how close together they are.

If no decimals, add default ones [duplicate]

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).

Categories