Round 2 decimals - php

I have an issue with php 5.6 rounding to two decimals. If you test the following it will export $y exactly as 19.620000000000001
I know there are solutions to display it using number_format, but how do I make it to be exactly 19.62?
Thanks.
$x = 19.620000000000001;
$y = round($x, 2);
var_export($y);
Later Edit.
I need it for exact math operations, not display.

Try using echo to display your output. It will give you your desired output

It's because you're using var_export
Try using echo or var_dump()
$x = 19.620000000000001;
$y = round($x, 2);
echo $y;

If you do number_format:
$y = number_format($x, 2);
The $y will have 2 decimals but it will also be a string. Not sure if it's satisfactory.

Related

Returning a "readable" number in PHP

I am trying to receive a result of a math operation with small numbers (maximum 8 decimals), I receive a float result, but in a format that make the other numbers stay with an error:
$a = round($x, 8); //returns 0.0478674, that's correct
$b = round($y,8); //returns 0.04786261, that's correct
$z = $a - $b; //z returns 4.7899999999976E-6, and not ‭0.00000479‬ as I was expecting
I tried as well
$w = round($z,8); //but w returns 4.79E-6, and not 0.00000479‬ as I was expecting
My problem is because the number 4.7899999999976E-6 give an error in other calcs and it's a ugly number to show to the user.
How can I make this number be 0.00000479‬?
you can use number_format:
$w = number_format($z,8);
number_format should do what you require, from the help: https://www.php.net/manual/en/function.number-format.php
For your specific requirement here:
$w = number_format($z,8);

php number_format display the decimal

I have a number like this 18914439.4524345860
and I want to display that number like this 18.91
I have try the number_format function
$x = 18914439.4524345860;
echo number_format($x, 2, ".", ".");
and the result is
18.914.439.45
how to show the number like this 18.91?
Thanks for your attention...
hope this help you :
$int = 18914439.4524345860/1000000;
echo round($int,2);
/* Output 18.91*/
18914439.4524345860 is a complete different number than 18.91, not just another representation.
You could divide by 1000000 first, and then use number_format:
$x = 18914439.4524345860;
echo number_format($x / 1000000, 2, ".", ".");

Unexpected round() return

I am trying to round down a number using PHP's round() function. Here is the code I am using:
$line_item_price = 13.775;
echo round($line_item_price, 2, PHP_ROUND_HALF_DOWN);
Now when I run the code like this I am hoping to get the output 13.77, except I am getting 0 (or nothing -- not sure which yet).
Now when I remove the PHP_ROUND_HALF_DOWN I get 13.78. Anyone see what I am doing wrong here? It seems like this should be working correctly.
The mode parameter was introduced in version 5.3, therefore it will not work for you. You'll have to find a custom function to do what you are looking for.
You are using a function that is not yet available in your current version of PHP. One way to solve this problem is using the floor function.
$line_item_price = 13.775;
echo floor($line_item_price * 100) / 100;
What I'm doing here is too first multiply the value with 100 and then floor the value. This will give you a rounded down value with the precision of 2. Then to get the correct value you need to devide with 100.
The number 100 comes from the power(10, desired precision)
can you not just do:
echo round($line_item_price, 2)
?
I'm not 100% sure but I think the ROUND_HALF_DOWN etc are for fractions such as 1.5, 2.5 and integers.
Here is a way to do it:
$num = 13.775;
$tmp = intval($num*1000);
$dec = $tmp % 10;
if ($dec > 5) {
$rounded = (1+intval($tmp/10))/100;
} else {
$rounded = intval($tmp/10)/100;
}
echo $rounded,"\n";
This gives : 13.77 for $num=13.775 and 13.78 for $num=13.776
Actually, I'm kind of surprised that it works at all, since the number 13.775 is not exactly representable in floating point:
$ php -r 'printf("%.40f\n", 13.775);'
13.7750000000000003552713678800500929355621
Indeed, it seems that round() is a bit lax about what counts as "half":
$ php -r 'echo round(13.77500000000001, 2, PHP_ROUND_HALF_DOWN) . "\n";'
13.77
Anyway, if your PHP doesn't support PHP_ROUND_HALF_DOWN, here's a simple kluge that gives approximately the same functionality:
function round_half_down ( $num, $digits ) {
$mul = pow( 10, $digits );
return ceil( $num * $mul - 0.5 ) / $mul;
}
This does turn out to work as one would naively expect, but is slightly stricter than round(): round_half_down(13.775, 2) == 13.77, but round_half_down(13.77500000000001, 2) == 13.78. Also, as a curious edge case, round_half_down(0.001, 2) returns -0. If you don't like that, you can always pass the return value through sprintf("%.{$digits}F") to format it nicely.

How to get the division part of a number

Say I have 1.234 I want to get the .234
I tried echo 1.234%1 //I get 0
I am rusty, what is the correct way?
(The tags says PHP as this might be an issue only with PHP, but I really am looking for the general solution).
php's % modulo operator converts its arguments to integers. To get a floating-point modulus, use fmod:
echo fmod(1.234, 1);
You can remove the whole number from the number itself. in php its:
$num = 1.234;
echo $num - floor($num);
Subtract the integer portion of $x ((int)$x) from $x:
$x = 1.234;
$d = $x - (int)$x;
// $d now equals 0.234
echo $d;
Example
Just substract integer part 1.234 - (int)1.234
Try this:
echo 1.234 - intval(1.234);

What's the best way to get the fractional part of a float in PHP?

How would you find the fractional part of a floating point number in PHP?
For example, if I have the value 1.25, I want to return 0.25.
$x = $x - floor($x)
$x = fmod($x, 1);
Here's a demo:
<?php
$x = 25.3333;
$x = fmod($x, 1);
var_dump($x);
Should ouptut
double(0.3333)
Credit.
Don't forget that you can't trust floating point arithmetic to be 100% accurate. If you're concerned about this, you'll want to look into the BCMath Arbitrary Precision Mathematics functions.
$x = 22.732423423423432;
$x = bcsub(abs($x),floor(abs($x)),20);
You could also hack on the string yourself
$x = 22.732423423423432;
$x = strstr ( $x, '.' );
The answer provided by nlucaroni will only work for positive numbers. A possible solution that works for both positive as well as negative numbers is:
$x = $x - intval($x)
If if the number is negative, you'll have to do this:
$x = abs($x) - floor(abs($x));
My PHP skills are lacking but you could minus the result of a floor from the original number
However, if you are dealing with something like perlin noise or another graphical representation, the solution which was accepted is correct. It will give you the fractional part from the lower number.
i.e:
.25 : 0 is integer below, fractional part is .25
-.25 : -1 is integer below, fractional part is .75
With the other solutions, you will repeat 0 as integer below, and worse, you will get reversed fractional values for all negative numbers.
Some of the preceding answers are partial. This, I believe, is what you need to handle all situations:
function getDecimalPart($floatNum) {
return abs($floatNum - intval($floatNum));
}
$decimalPart = getDecimalPart($floatNum);
You can use fmod function:
$y = fmod($x, 1); //$x = 1.25 $y = 0.25
To stop the confusion on this page actually this is the best answer, which is fast and works for both positive and negative values of $x:
$frac=($x<0) ? $x-ceil($x) : $x-floor($x);
I ran speed tests of 10 million computations on PHP 7.2.15 and even though both solutions give the same results, fmod is slower than floor/ceil.
$frac=($x<0) ? $x-ceil($x) : $x-floor($x);
-> 490-510 ms (depending on the sign of $x)
$frac=fmod($x, 1);
-> 590 - 1000 ms (depending on the value of $x)
Whereas the actual empty loop itself takes 80 ms (which is included in above timings).
Test script:
$x=sqrt(2)-0.41421356237;
$time_start = microtime(true);
for ($i=0;$i<=9999999;$i++) {
//$frac=fmod($x, 1); // version a
$frac=($x<0) ? $x-ceil($x) : $x-floor($x); // version b
}
$time_end = microtime(true);
$time = $time_end - $time_start;

Categories