Why do I need to save all the integers involved in the amount?

Source: Internet
Author: User
Tags gmp mathematical functions
For example, if the amount is 100.02, the database requires 10002 storage. For example, if the amount is 100.02, the database requires 10002 storage.

Reply content:

For example, if the amount is 100.02, the database must be saved as 10002

Hope to help you:

  • Code mystery (4)-floating point number (from surprise to thinking)
  • Code mystery (5)-floating point number (who stole your precision ?)

Float computing is not accurate

Why do we need an integer? UseDecimal Field TypeNo? If computing is involved, you can useBC mathematical functions. Note: currently, floating point numbers cannot be fully and precisely stored in computers, and can only be maximized. Unless it is a floating point number that you read from the database or obtained from the outside, such as the GET parameter.

In actual projects, if the product price involves addition, subtraction, multiplication, division, and so on, some rules will also be selected. For example: rounding up, increasing up, bankers rounding up, etc.

The integer is accurate, and the speed is fast...

If the floating point number is not accurate, the calculation may be biased.

Floating Point precision:
Http://php.net/manual/zh/language.types.float.php
Rational numbers that can be accurately expressed in decimal form, such as 0.1 or 0.7. No matter how many tails are there, they cannot be accurately expressed by the binary used internally.
Therefore, the binary format cannot be converted without a slight loss of precision.
This results in confusion. For example, floor (0.1 + 0.7) * 10) usually returns 7 instead of the expected 8.
Because the internal representation of the result is similar to 9999999999999991118...
Never believe that the result of a floating point number is accurate to the last digit, or compare whether two floating point numbers are equal.
If higher accuracy is required, use the bcmath (Binary Calculator Math) function or the gmp (GNU Multiple Precision) function.
Http://php.net/manual/zh/ref.bc.php
Http://php.net/manual/zh/ref.gmp.php

To be safe, we should use strings to store large integers and use mathematical libraries such as bcmath for calculation.
For example, when using PHP to calculate the account balance, you can use the bcmath series of mathematical computing functions with any precision.
Add bcadd
Bcsub reduction
Multiply bcmul
Except bcp
Multiply by bcpow
Square Root bcsqrt
Comparison of bccomp
Modulo (remainder) bcmod


  

Floating point numbers have precision problems. The float and double types of database fields are prone to errors.
If you store data with high precision requirements, such as account balance, you cannot use the float type. In this case, you should use the decimal type.
For example, we define the data type of the MySQL database Field "account balance" as decimal (999.99), so that we can store the number in the range of-999.99 to without any error. the number on the left of the brackets indicates the total number of valid digits, and the number on the right indicates the number of decimal places. if the range is not enough, it can be defined as decimal (10, 2 ).

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.