1

I want my variable's first decimal to always be rounded up. For example:

9.66 goes to 9.7
9.55 goes to 9.6
9.51 goes to 9.6
9.00000001 goes to 9.1

How do I do this?

Maarten Wolfsen
  • 1,625
  • 3
  • 19
  • 35

5 Answers5

2

Use round() with an optional precision and round type arguments, e.g.:

round($value, 1, PHP_ROUND_HALF_UP)

The optional second argument to round() is the precision argument and it specifies the number of decimal digits to round to. The third optional argument specifies the rounding mode. See the PHP manual for round for details.

Using round() does not always round up, even when using PHP_ROUND_HALF_UP (e.g. 9.00001 is not rounded to 9.1). You could instead try to use multiplication, ceil() and division:

ceil($value * 10.0) / 10.0

Since these are floating-point values, you might not get exact results.

jotik
  • 17,044
  • 13
  • 58
  • 123
  • I already used round, but it always rounds down when the third (or further) decimal is lower then 5. I also looked into the precision arguments, but couldn't find anything useful. – Maarten Wolfsen May 12 '16 at 07:53
  • I think it does not work exactly as specified. Fails with `echo '9.00000001 => '.round(9.00000001, 1, PHP_ROUND_HALF_UP).PHP_EOL;` for me output is 9. – Victor Smirnov May 12 '16 at 07:59
  • [This question](http://stackoverflow.com/q/36324171/3919155) might also help you, but I edited my answer (again) to provide a simple alternative solution to your problem. – jotik May 12 '16 at 08:05
  • Well, it works pretty good. The number 9.0000001 was an example, but this number will probably never appear. Thanks! – Maarten Wolfsen May 12 '16 at 08:08
1

I'm not a php programmer so will have to answer in "steps". The problem you have is the edge case where you have a number with exactly one decimal. (e.g. 9.5)

Here's how you could do it:

  1. Multiply your number by 10.
  2. If that's an integer, then return the original number (that's the edge case), else continue as follows:
  3. Add 0.5
  4. Round that in the normal way to an integer (i.e. "a.5" rounds up).
  5. Divide the result by 10.

For step (2), sniffing around the php documentation reveals a function bool is_int ( mixed $var ) to test for an integer.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

I made couple tests and suggest the following answer with test cases

<?php

echo '9.66 (expected 9.7) => '.myRound(9.66).PHP_EOL;
echo '9.55 (expected 9.6) => '.myRound(9.55).PHP_EOL;
echo '9.51 (expected 9.6) => '.myRound(9.51).PHP_EOL;
echo '9.00000001 (expected 9.1) => '.myRound(9.00000001).PHP_EOL;
echo '9.9 (expected ??) => '.myRound(9.9).PHP_EOL;
echo '9.91 (expected ??) => '.myRound(9.91).PHP_EOL;

function myRound($value)
{
    return ceil($value*10)/10;
}
Victor Smirnov
  • 3,450
  • 4
  • 30
  • 49
0

You will need a custom ceil() function, your requirements cannot be satisfied by the default function or by the round.

Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
0

Use this: online test

You can use this technique. Just explode the given number / string, get the number which is next value / digit of the .. after getting this you need to increment that value and check if the value is greater than 9 or nor, if then divide that and add the carry to the first portion of the main number.

$var = '9.96';
$ar = explode(".", $var);

$nxt = substr($ar[1], 0, 1) + 1;

if($nxt > 9){
    $tmp = (string) $nxt;
    $num = floatval(($ar[0] + $tmp[0]).".".$tmp[1]);
}   
else
    $num = floatval($ar[0].".".$nxt);

var_dump($num); // float(10)
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42