Golang rounding up, rounding down, and rounding

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

I. Overview

The official math package provides the rounding method, rounding up math.Ceil() , rounding down math.Floor()

Second, usage

1
2
3
4
5
6
7
8
9
10
 Package Main
Import (
"FMT"
"Math"
)
funcmain() {
1.1
Fmt. Println (Math. Ceil (x)) //2
Fmt. Println (Math. Floor (x)) //1
}

It is important to note that the complete return is not a real integer, but a float64 type, so int a manual conversion is required if the type is required.

2017-10-14 Append: A Wonderful rounding method

Golang no Python-like round () function, search a lot of it is very complex, and finally saw a fresh and refined: first +0.5, then take the whole down!

It's incredibly easy to think, and there's nothing wrong with that.

1
2
3
 funcroundfloat64)  {
return int 0/5))
}
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.