I have an f32 value that I'd like to print. Being a float, I can represent integers with this type as well
let a_float: f32 = 3.0; let another_float: f32 = 3.14; // let's pretend this was user input, // we didn't know what they'd put enter, so we used f32 to cover our bases let an_integer: f32 = 3; I'd like to print a value stored as an f32 with a minimum amount of precision, but using as much as necessary to represent the value stored. If my desired minimum precision was one (1), I'd expect the following transformation to be done on the float:
let a_float: f32 = 3.0; // print 3.0 let another_float: f32 = 3.14; // print 3.14 let an_integer: f32 = 3; // print 3.0 I know that I can set a finite number of decimal places using std::fmt's precision, but that doesn't seem to give me what I want. Is there a way to achieve this functionality without bringing in additional formatting crates? Pulling in additional crates isn't out of the realm of possibility, I'm moreso interested in what I'm able to do 'out of the box'
https://stackoverflow.com/questions/65947428/rust-formatting-a-float-with-a-minimum-number-of-decimal-points January 29, 2021 at 09:06AM
没有评论:
发表评论