How to round numbers? - php

7000 / 253000 = 0.027667984189723
$number1 = "7000";
$number2 = "253000";
$total = $number1 / $number2;
// return 0.027667984189723
This result how to round 2766 ?

Try this
echo round($total,5);

$number1 / $number2 * 10000 % 1

I'm not sure how you came up with 2766 as a result of your decimal. But I'm up for that challenge. Firstly, to get the correct decimal placing do:
<?php echo round($total, 5); ?>
However, if you're like me, I'm going to figure out that challenge and have some fun with it:
$num1 = round($total, 5);
$num2 = strval($num1);
$num3 = str_split($num2);
$arr1 = array();
foreach ($num3 as $num) {
if(is_string($num)) {
array_push($arr1, $num);
}
}
$arr2 = array_slice($arr1, 3);
foreach($arr2 as $out) {
echo $out;
}
The expected output is 2767, since I did it with the correct round function.

I hope it works for you i tried with different values its working good when value start with 0.
$after_point = ltrim($total,"0."); //27667984189723
echo substr($v,0,4); //2766
if you give 52.548457 then it return 52.5
You must chack that number start with 0.

Related

Is there a specific function that allows me to round up to a specific decimal number in PHP

I have a number that needs to be rounded up to a specific decimal, is there any function in PHP to do that?
I need every number (which reflects an amount of money) to have a specific decimal number.
For example:
The decimal needs to be 25, so if I got $ 25.50 I need it to be $ 26.25, and if I got $ 25.10 it needs to be $ 25.25.
I've checked PHP round(), and specifically ceil(), and I've come across this answer in Python, but I'm not sure it applies to my case, because what I need is different.
Any ideas? Even pseudo code as a tip on where to start will help me. Thanks!
I think you need a custom function, something like this:
function my_round($number, $decimal = 0.25) {
$result = floor($number) + $decimal;
if ($result < $number) $result = ceil($number) + $decimal;
return $result;
}
print my_round(25.50);
I modified this answer for your case:
<?php
function roundUp($number){
$int = floor($number);
$float = $number-$int;
if ($float*10 < 2.5)
$result = $int;
else
$result = ceil($number);
$result+= 0.25;
echo $number." becomes ".$result."\n";
}
roundUp(25.50);
roundUp(25.10);
Look for demo here
Following axiac's advice mentioned in the comments and following this thread, the best way to deal with floating point numbers in the context of currencies, is to treat the dollars and cents' values as 2 separate entities.
One way I can think of it to split the numbers before and after the decimal into 2 separate variables and process accordingly.
<?php
function customRound($amount){
$amount = strval($amount);
if(preg_match('/(\d+)\.?(\d{1,2})?/', $amount, $matches) !== 1){
throw new \Exception("Invalid amount.");
}
$dollars = intval($matches[1]);
$cents = intval($matches[2] ?? 0);
if($cents < 10) $cents *= 10;
if($cents <= 25) return $dollars . ".25";
return ($dollars + 1) . ".25";
}
$tests = [25.51,25.49,26.25,25.10,25.49];
foreach ($tests as $test){
echo $test," => ",customRound($test),PHP_EOL;
}
Here's another approach:
<?php
function roundUp($number, $decimal=0.25){
$dollars = floor($number);
$cents = $number - $dollars;
if($cents > $decimal) {
++$dollars;
}
return $dollars + $decimal;
}
echo roundUp(25.50).PHP_EOL;
echo roundUp(25.10);

number_format() to eliminate .06

I am having trouble of eliminating .06 using number_format()
what I want to do is:
56.00 => 56
56.10 => 56.10
56.06 => 56
I did it with regex but I can't eliminate it, maybe my statement is wrong.
$num = 50.06;
$num = preg_replace("/\.0*$/",'',$num);
I need your help
Thanks in advance
The regex:
/\.0[0-9]+/
Working DEMO.
So:
$num1 = 50.06;
$num1 = preg_replace("/\.0[0-9]+/",'',$num1); //50
$num2 = 50.10;
$num2 = preg_replace("/\.0[0-9]+/",'',$num2); //50.1
Reference:
preg_replace()
This may be stupid as I'm tired and ready to head out, but:
if(($num - intval($num)) > .1) {
$num = number_format($num, 2);
} else {
$num = number_format($num, 0);
}
The correct regex is \.0[0-9]{1}$
The last param of preg_replace is $num
$num = 56.06;
echo $num = number_format($num, 0);

Round number in php depending on the value

I know the following code will remove the numbers are the point.
round($number);
I want to round the numbers as follows
if number is 20.123
I want result 20,
If number is 20.567
I want result 21
Means if value is below .5 , it should remove that value.
If value if .5 or above it should round up.
How ?
Anyone help ?
round($number) will do what you want:
round(20.156); // 20
round(20.651); // 21
Live example
I am sure it's working well.
<?php
$var = 22.443;
$var = number_format($var, 0, '.', '');
echo $var;
?>
function get_decimal_num($number){
$num = explode('.', $number);
return $num[1];
}
$num = 10.5 ; // for example
$val = get_decimal_num($num);
if($val >= 5)
{
$value = (int) $num;
echo $value = $value + 1;
}
if($val < 5)
{
echo $num;
}
Plese check it...
Try this:
use the ceil, floor, explode and substr function to achieve you value.
$num = "20.123";
$arr = explode(".", $num);
if(substr($arr[1], 0, 1) >= 5){
$num = ceil($num);
}else{
$num = floor($num);
}
echo $num;
Result:
20
Also you can use the round function.
round(20.156); // 20
round(20.651); // 21

Round small numbers

I would like to know how to achieve this:
round(0.38)
And receive this:
0.40
How can I do this? I tried with round(0.38, 2) but it does not work.
To get 0.4, round to 1 decimal place using round() where the second parameter is the decimal place.
$rounded_number = round(0.38, 1);
To get 0.40, use number_format() to get 2 decimal places.
$formatted_number = number_format( $rounded_number , 2);
Multiply by ten, round, then divide by ten:
php > $a = 0.38;
php > $b = $a * 10;
php > $c = round($b);
php > echo $c/10;
0.4
If you want the two decimal places:
$num = 0.38;
$num = round($num * 10)/10;
$num = number_format($num, 2);
echo $num;
It is probably far simpler than you think it is:
$num = round(0.38 * 10)/10;
echo $num;
Then, you could just replace 0.38 with another number or a variable.
$a = 0.38;
echo sprintf("%.2F", round($a,PHP_ROUND_HALF_UP));

PHP math (numbering)

$temp is currently 6. But the variable result can be changing every time to a different number so it is not a fixed value.
Anyway, for this $temp * 1.1666666, the result will be 6.99999996. Since I used the floor function, it will be rounded down to 6.
Is there any way when the value is more then>*.49999 it will stay at *.5 instead of *?
Example: 6.51111111, 6.78948123, 6.9747124
Expected Output: 6.5
Example: 6.49999999, 6.12412431, 6.33452361
Expected Output: 6
Do note that, $temp value will be ever changing..thank you!
Use round($number, 1). That will round to the nearest decimal point.
$number = round(.1666666 * $temp, 1);
If you want to round to the nearest half you can do this:
function round_to_half($num)
{
if($num >= ($half = ($ceil = ceil($num))- 0.5) + 0.25) return $ceil;
else if($num < $half - 0.25) return floor($num);
else return $half;
}
$number = round_to_half(.1666666 * $temp);
Try this code...
<?php
$temp = 6.94444;
echo myRound($temp);
function myRound($temp)
{
$frac = $temp - floor($temp);
$frac = ($frac >= .5) ? .5 : 0;
return ( floor($temp) + $frac );
}
?>
Hope this is what you want.

Categories