For example, I have several numbers (the number of digits after the decimal point is not fixed):
1, 155.055
2, 122.196
3, 0.9631
What can I do to get these numbers to the last digit of the decimal +1, or-1? For example, + 1 would like to get:
1, 155.056
2, 122.197
3, 0.9632
Reply content:
For example, I have several numbers (the number of digits after the decimal point is not fixed):
1, 155.055
2, 122.196
3, 0.9631
What can I do to get these numbers to the last digit of the decimal +1, or-1? For example, + 1 would like to get:
1, 155.056
2, 122.197
3, 0.9632
$num =12.2346;$tmp = explode('.',$num);$dec = end($tmp);$count = strlen($dec);$p= pow(0.1,$count);$result = $num+$p;echo($result);
Change yourself into a function, some places have to add some judgment,
are saved as integers, and then stored in a decimal place.
Because the float type still cannot accurately represent 0.1 0.01 0.001 ... such as data, multiple additions and deletions will have accumulated error, even add the same as no Add.
Like what
0.1+0.2 == 0.30000000000000004
$n = 10.123;
$fix = Floatval (Pow (strlen) (Explode ('. ', Strval ($n)) [1]));
$n = ($n * $fix + 1)/$fix;