Golang default rounding rules for floating-point types--four six into 50% pairs

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Four homes six into 50% pairs is a more accurate and scientific method of counting retention, which is a kind of digital revision rule, aka Banker rounding method. It is more accurate than the usual rounding method.

Specific rules:

    1. The number to be repaired is less than 5 o'clock, and the number is shed;

    2. If the number of the repaired is greater than 5 o'clock, the rounding is taken;

    3. The number of the repair is equal to 5 o'clock, to see the number of the front 5, if the odd is carry, if even the number will be 5 off, that is, after the end of the revision of the figure is an even number, if there is no "0" in the back of any numbers, then regardless of whether the front of the 5 is an odd or even,

Mnemonic formula:

four six into five consideration, five after non-zero into one, five after the zero look at the odd even, five ago for I should give up, five ago for odd to go into a

Golang in floating-point type by default using the Banker rounding method, such as the following using code validation example

import ("fmt")func main() {fmt.Printf("9.8249=>%0.2f(四舍)\n", 9.8249)fmt.Printf("9.82671=>%0.2f(六入)\n", 9.82671)fmt.Printf("9.8351=>%0.2f(五后非零就进一)\n", 9.8351)fmt.Printf("9.82501=>%0.2f(五后非零就进一)\n", 9.82501)fmt.Printf("9.8250=>%0.2f(五后为零看奇偶,五前为偶应舍去)\n", 9.8250)fmt.Printf("9.8350=>%0.2f(五后为零看奇偶,五前为奇要进一)\n", 9.8350)}

Output results

  9.8249  =>  9.82(四舍)  9.82671 =>  9.83(六入)  9.8351  =>  9.84(五后非零就进一)  9.82501 =>  9.83(五后非零就进一)  9.8250  =>  9.82(五后为零看奇偶,五前为偶应舍去)  9.8350  =>  9.84(五后为零看奇偶,五前为奇要进一)

Therefore, it is convenient for me to use the FMT method to make the banker trade-offs of floating-point types and obtain their approximate numbers. For example:

import ("fmt""strconv")func main() {s := fmt.Sprintf("%0.6f", 17.82671567890123456789987654324567898765432)f, _ := strconv.ParseFloat(s, 64)fmt.Println(s, f)}

Output results

  17.826716 17.826716

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.