英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论