Rounding numbers

I am trying to work a dividing problem and then give the user the correct answer in the event they entered it incorrectly. However, the two random numbers I generated are i32 and I'm going to need to round the answer to 3 decimal places. I came up with the following code but it gives an answer of zero everytime.

let correctans = f64::from( ( secondnum / firstnum ) * 1000 ).round() / 1000.0;

I have to use the f64 in order to call round and this was all I could find to typecast an int to a float although I would think diving two ints would make a float. This runs fine but it gives a zero answer every time.

What is the proper method to use to deal with this situation in rust?



Try converting both numbers to f64 before doing the division:

let correctans = (f64::from(secondnum) / f64::from(firstnum) * 1000.0).round() / 1000.0;

same result.

Have you checked the values of your input numbers? When I test this out, it seems to work. For example, the following program prints "53 / 130 = 0.408":

fn main() {
    let a = 53;
    let b = 130;
    let c = (f64::from(a) / f64::from(b) * 1000.0).round() / 1000.0;
    println!("{} / {} = {}", a, b, c);

Run this code on Playground.

1 Like

Ok, it is working but I have a different problem now. Maybe my original code was also working. The problem now is that I declare correctans above as

let correctans: i32;

I then use it for a couple different types of math problems, ie: addition, subtraction, multiplication.

However, if I am dealing with division I was going to shadow the original variable and say

let correctans = our rounding code above.

The problem when I do this though is that it is scoped within my if dividing code and when I leave it we end up printing the earlier variable which is still zero.

So now to find a work around this. :slight_smile:

I got it figured out.

Thanks for you help,


While it is legitimate syntax, you'll almost never see Rust code forward-declare a variable like you would in C. Instead, just create the variable where it is used.

It's also not a good idea to reuse the same variable for multiple things unless they represent the same concept.

For example, the result of addition and subtraction often mean very different things (e.g. one is a sum or total, the other is usually a difference), so you wouldn't normally want to store the result of an addition or subtraction in the same variable.

I've seen loads of non-Rust code where they'll try to reuse the same variable (usually called something useless like x or temp or or thisPoint) for different operations and it has a massive negative impact on readability and maintainability.

Often it means you can't rely on the variable having a good name and instead need to read the code in detail to figure out what it means at a particular point in your code. This can be annoying and error-prone.

LLVM also has an optimisation where it'll recognise when two variables aren't used at the same time and their stack memory can be used. So often creating loads of variables won't have an impact at runtime.

1 Like

If your problem is a / b = c, then how about generating b and c and computing a = b * c?
That solves the problem of half the answers being below one.

If you do want a remainder, then generate the remainder in the range of 0 .. b and add it to b * c.

Thanks, I finally got it working but I was forward declaring because of scope issues. After it finishes with the calculation I needed it outside the loop to present the user with the correct answer if they missed it. If I declared the variable inside the loop or if statement then I wouldn’t have access to it later.

I could have written 4 different functions to deal with this I know but it wasn’t really too complicated to do in 1 function and after I worked through the variable issue I got it all working. I did end up creating another variable to deal with dividing though so that I didn’t have to use shadowing.

I’m not sure I follow your logic on reusing the same var for addition and subtraction or on shadowing being frowned on. Rust is the only typed language I’ve ever seen that lets you redeclare the same variable in order to change its type so figured it would be ok to do it based on that fact. If there is an issue with doing that what is it so that I can stay away from that in the future.



This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.