简体   繁体   中英

rounding a decimal in C# based on user input

I'm trying to round decimals based on the user input instead of predefined values.

eg User inputs 1.2345, then they input the amount of decimals they need it rounded to like say 3. It would then round it and output 1.236

However, I can't for the life of me figure out how to round decimals based on input, only predefined values.

What would be the best way to do something like this?

Try Math.Round

var input = 3;
var value = 1.2345;

var result = Math.Round(value, input, MidpointRounding.AwayFromZero);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM