The project I am working on frequently converts between different units of integers. It is very common to write
pub const DURATION_1MIN: i64 = 60 * 1_000_000_000; let timestamp_ns = timestamp_s * 1_000_000_000; let fract = (d.fract() * Decimal::new(1000000000000000000, 0)).round();
The underscore works well for up to millions (
7_000_000) in terms of readability.
It does not help reading/writing a literal longer than this (
This is error-prone and hard to code-review.
However the compiler does not allow the scientific notation for integers:
let killo: i64 = peta * 1e12
I am reluctant to write an explicit cast
let a:i64 = 1e15 as i64 // is this safe?
because I am not sure it might be subject to floating errors like
0.999..e15 and converted to
Even more useful notation is
let a: i64 = 123.45e15. This is still a valid whole number as long as the number of fractional digits before 'e' is not greater than 15.
// integer literal for various types let a: i64 = 10; let a: i32 = 10; // scientific notation for various types let a: f64 = 123.45e15; let a: i64 = 123.45e15; let a: i64 = 123000e-3; let a: i64 = 123.456e2; // compile error const TIME_LIMIT_NS: i64 = 1.5e9; // 1.5 seconds const TIME_LIMIT_NS: i64 = 1_500_000_000; // 1.5 seconds