database field design for floating-point numbers such as amount, weight, etc. (calculated using Int,long instead of floating-point numbers)

Source: Internet
Author: User

Database fields such as amount, weight, and score are recommended for use with an int or bigint type.

Usually our database design in the amount, weight and other fields related to the decimal place will be used float or decimal,mysql can also use double, but often each amount of calculation we only need to accurate to the minute, the weight is accurate to gram, the best design is to use the int type to replace the floating-point type, If the number involved is larger than the value range of int ( -2,147,483,648~2,147,483,647), the maximum amount is 21474836.47, which is more than 20 million, or 2147483.647 kg, which we can use bigint,c# Using the long type (9,223,372,036,854,775,808~9,223,372,036,854,775,807) in the The maximum amount of 92233720368547758.07, billion, the national fiscal revenue is trillion, so generally enough to use, if they are positive, you can also use ulong, imagine, if more than ulong type how to deal with, we can think about it?

The advantage of using int instead of floating-point numbers is that the int type is much faster than the calculation speed, and the number of people is much faster, but with the current computer computing ability, the general system can ignore this effect; The greatest advantage of using int is to solve the problem of computational accuracy, and the floating-point number is biased when the total rounding of N records It's also more troublesome in data addition and subtraction. For example: A customer account database balance of 123.2301 yuan, we display in the interface only need to display 123.23 yuan, if the customer consumes 123.23 yuan, the actual balance of 0, but the database also has 0.0001 yuan, so we need to deal with this extra 0.0001 yuan in the code, some people will say that the accuracy of writing Rounding is good, but still involves the processing of precision and deviation from the total amount (the number of records rounded up with the actual total amount will be biased), if the use of type int instead of floating point, we just have to do a format processing.

database field design for floating-point numbers such as amount, weight, etc. (calculated using Int,long instead of floating-point numbers)

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.