PHP last digit plus 1, minus 1

Source: Internet
Author: User
Keywords Php
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;

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.