Add 1 to the last digit of the PHP decimal point, minus 1

Source: Internet
Author: User
For example, how many digits do I have (the digits behind the decimal point are not fixed): 1, 155.0552, 122.1963, 0.9631? How can I make the last decimal point of these digits + 1, or-1? For example, in the case of + 1, we hope to get: 1, 155.0562, 122.1973, and 0.9632. For example, I have several numbers (the digits after the decimal point are not fixed ):

1. 155.055
2. 122.196
3. 0.9631

What can I do to make the last decimal point of these numbers + 1, or-1? For example, in the case of + 1, we hope to get:

1. 155.056
2. 122.197
3. 0.9632

Reply content:

For example, I have several numbers (the digits after the decimal point are not fixed ):

1. 155.055
2. 122.196
3. 0.9631

What can I do to make the last decimal point of these numbers + 1, or-1? For example, in the case of + 1, we hope 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 it to a function, and add some judgment in some places,

Store them as integers, and then store a decimal point.
Because float type still cannot accurately represent 0.1 0.01 0.001... When data is added or deleted multiple times, there will be a cumulative error, or even the addition is the same as the addition.
For example

0.1+0.2 == 0.30000000000000004

$ N = 10.123;
$ Fix = floatval (pow (10, strlen (explode ('.', strval ($ n) [1]);
$ N = ($ n * $ fix + 1)/$ fix;

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.