将小数四舍五入到三位小数点。

huangapple go评论69阅读模式
英文:

How to round decimal to 3 decimal places?

问题

I have variable result. It contains each time different value. It is a decimal number. How can I round or just show 3 decimal places?

Code:

var result = amount * exchangeRate1[fromCurrency] / exchangeRate2[toCurrency];
英文:

I have variable result. It contains each time different value. It is decimal number. How can i round or just show 3 decimal places?

Code:

var result = amount * exchangeRate1[fromCurrency] / exchangeRate2[toCurrency];

答案1

得分: 2

if you want to SHOW 3 decimal places, you have to format value to string

 string val = Math.Round(result,3).ToString("0.000"); // 2.000

if you use just Round

result = Math.Round(result,3); // 2

it will be shown less than 3 decimal places, if your result has less than 3 decimal places.

英文:

if you want to SHOW 3 decimal places, you have to format value to string

 string val = Math.Round(result,3).ToString("0.000"); // 2.000

if you use just Round

result = Math.Round(result,3) // 2

it will be shown less then 3 decimal places, if your result has less than 3 decimal places.

答案2

得分: 1

Math.Round(result, 3);

英文:

Try this:

Math.Round(result, 3);

huangapple
  • 本文由 发表于 2023年4月10日 18:43:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定