Logo

Programming-Idioms

History of Idiom 81 > diff from v17 to v18

Edit summary for version 18 by :
[Go] Proper import

Version 17

2016-01-03, 13:25:48

Version 18

2016-01-03, 13:31:38

Idiom #81 Round floating point number to integer

Declare integer y and initialize it with the rounded value of floating point number x .
Ties (when the fractional part of x is exactly .5) must be rounded up (to positive infinity).

Idiom #81 Round floating point number to integer

Declare integer y and initialize it with the rounded value of floating point number x .
Ties (when the fractional part of x is exactly .5) must be rounded up (to positive infinity).

Code
let y = x.round() as i32;
Doc URL
http://doc.rust-lang.org/std/primitive.f64.html#method.round
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20x%20%3D%2041.5f64%3B%0A%20%20%20%20let%20y%20%3D%20x.round()%20as%20i32%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20y)%3B%0A%7D&version=stable