Round second digit after decimal to produce nice number - php

Need to round 30.61 to 30.60, Any built-in function for PHP to do this ?

If I understand your desired output correctly, that you only want to round the second decimal point, you can round with 1 decimal presicion, then use numer_format() to ensure you get the correct number of decimals.
$num = 30.61;
echo number_format(round($num, 1), 2);
round() documentation
number_format() documentation
Live demo

you can do this
$num = 3.61;
/*round to nearest decimal place*/
$test_number = round($num,1);
/* ans :3.6
format to 2 decimal place*/
$test_number = sprintf ("%.2f", $test_number);
/* ans : 3.60 */

Related

How to transform last two numbers to decimal (int to float) in php?

I need transform 100 to 1.00, 345 to 3.45 or any number with 3 digits or more to record in db like a decimal.
Don't need add .00, just transform last two numbers in decimal.
I try number_format($num, 2) but is wrong.
It seems, that you want to divide your numbers to 100, so using number_format() with the appropriate $decimal_separator and $thousands_separator is an option:
<?php
echo number_format($num / 100, 2, '.', '');
?>
Since you are converting from an integer to a float, you can achieve this simply by dividing the number by 100.
$input = 100;
$value = floatval($input) / 100;
$value = number_format($value, 2);
echo $value;
You may then use number_format (as you were using before) to force two decimal places after any evenly divided float numbers (such as 100).
Demo: PHP Sandbox Example

PHP format number without using number_format function

AS the title says: using this number 1000000.125685, how can I format it like 1,000,000.125 without using number_function, since that function rounds the decimals?
number_format("1000000.125685",3) //returns 1,000,000.126
How can I give whatever number I want 'Money Format' without using number_format?
If you use floor() on the number prior to formatting it, this will get what you want. The problem is that floor() rounds fractions and not 'to n decimal places'. So this code does the round trip of *1000 and then /1000...
echo number_format(floor(1000000.125685*1000)/1000,3);
gives...
1,000,000.125
To wrap this up into a function with the number and decimal places as parameters...
function number_format2( float $n, int $dp ): string {
$multi = pow(10, $dp);
return number_format(floor($n*$multi)/$multi,$dp);
}
echo number_format2(1000000.125685,4);

is possible to less float values..?

$quantity = 20;
$product_rate = 66.79;
$total = $quantity * $product_rate;
echo $total;
Output is showing 1335.8000000000002
is there possible to show 1335.8 using php..?
You can use the number_format() function like this:
$firstNum = 1335.8000000000002;
$number = number_format($firstNum, 1, '.', '');
echo $number;
outputs:
1335.8
more on number_format() here: http://php.net/number-format.
You can also multiply the number by 10, then use intval() to convert it to an integer (that way stripping out the decimals) and then divide by 10 like this:
$firstNum = 1335.8000000000002;
$number = 10 * intval($firstNum)/10;
echo $number;
outputs:
1335.8
Note: when using the methods above there will be no rounding, for rounding you would use something like this:
$number = round($firstNum, 1);
echo $number;
which in this case also outputs:
1335.8
Do you really use these variable values? I'm using PHP7 and the output for your given values is 1335.8. If you do a manual calculation it is the same result. It should be 1335.8. Anyway if you need to roundup the value you can use below.
round($total,1);
Please refer the below link and you will be able to grab more details.
http://php.net/manual/en/function.round.php
Because how floating point numbers work, they cannot represent every numbers exactly, so approximations are made.
The closest representation of 20 is 20, it can represent 20 exactly, but 66.79 for instance is approximated to 66.7900000000000062527760746889, that times 20 is 1335.800000000000125055521493778 that again cannot be represented and is approximated to 1335.80000000000018189894035459.
Depending on how you choose to print this number, it may round different ways, in your case for some reason you decided to print 13 decimal places so it rounded to 1335.8000000000002, but if you print only 1 or 2 decimal places it will print as 1335.8 or 1335.80. Just be mindful about that when printing floating point numbers, you may want to specify how many decimal places are relevant to you. For that, use number_format().
Example:
echo number_format($number, 2); // prints 2 decimal places
You can do this simply using echo echo round($total, 1) instead of doing round($total)

Best way to convert a decimal number to next non-decimal number in PHP

Example: if a number is: 3.2 then it will become 4
round($variable), function only do this, if the decimal value is 0.6 or higher.
Is their any function in PHP to do so? Currently, i am using this code to do what i want:
if ($totalpages > round($totalpages)) // If the number is less than 0.06
{
$round1 = round($totalpages);
$round2 = $totalpages-round($totalpages);
$round3 = 1-$round2;
$round4 = $totalpages+$round3;
$totalpages = $round4; //
}
else
$totalpages = round($totalpages);
Is their any better way to do so?
Your looking for ceil()
ceil — Round fractions up
Description
float ceil ( float $value ) Returns the next highest integer value by
rounding up value if necessary.
http://php.net/manual/en/function.ceil.php

want to display exactly 2 digits after floating point

I want to convert floating value of 8 digits after floating point in to 2 digits after floating point ..
Eg. $a = 2.200000 ==> 2.20
I am using round function of php. Problem with round is if my number is 2.200000 it converts number in 2.2 . I want output as 2.20
Can any one suggest the possible way?
Actual code
$price = sprintf ("%.2f", round(($opt->price_value + ($opt->price_value * $this->row->prices[0]->taxes[0]->tax_rate)), 2));
i want out put like if my floating number is 2.2000000. then it should return me 2.20. but right now it is returning me 2.2
This does what I think you are asking for:
<?php
$a = 2.20032324;
$f = sprintf ("%.2f", $a);
echo "$a rounded to 2 decimal places is '$f'\n";
$a = 2.2000000;
$f = sprintf ("%.2f", $a);
echo "$a rounded to 2 decimal places is '$f'\n";
results:
[wally#lenovoR61 ~]$ php t.php
2.20032324 rounded to 2 decimal places is '2.20'
2.2 rounded to 2 decimal places is '2.20'
I added two test cases
i was working on it :D
$number='49.099998479854654656516198498465465465465';
function pure_decimal($number){
if(is_numeric($number) and floor($number)!=$number){
//decimal detected
$explode=explode('.',$number);
$first_no=$explode['0'];
$second_no=$explode['1'];
$get_only_two=substr($second_no,0,2);
$final_no=$first_no.'.'.$get_only_two;
$final_no=($final_no*1);
}else{
//normal
$final_no=$number;
}
return $final_no;
}
//result is 49.09
regards.
try $val = round($a * 100)/100;
Try the below code, You can get your result.
$a = 2.200000;
echo number_format((float)$a,2,'.','');

Categories