防止双尾的尾随零 [英] Prevent trailing zero's in double

查看:72
本文介绍了防止双尾的尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在将double转换为字符串时遇到问题并防止尾随零。



有一个变量double maxPrice它没有定义它可以有多少小数位。有时像12.30或12.20或12.120。所以我需要以字符串格式转换相同的值并在报表中显示。但是当我尝试转换它会截断尾随零。



请告诉我如何防止这种情况。



我尝试过:



两位数= 12.00d;



Console.WriteLine(digit.ToString(CultureInfo.InvariantCulture));

Hi,
I have a problem during conversion of double to string and prevent trailing zeros.

there is a variable double maxPrice which have not defined how many decimal places it can. sometime like 12.30 sometime 12.00 or 12.120. So i need to convert the same value in string format and show in a report. but when i try to convert it truncates the trailing zero's.

Please let me know how i can prevent this.

What I have tried:

double digit = 12.00d;

Console.WriteLine(digit.ToString(CultureInfo.InvariantCulture));

推荐答案

它看起来是G29格式说明符应该可以解决问题,例如: c# - 在没有尾随零的情况下显示小数的最佳方法 - 堆栈溢出 [ ^ ]。
It looks the "G29" format specifier should do the trick, see, for instance: c# - Best way to display decimal without trailing zeroes - Stack Overflow[^].


这篇关于防止双尾的尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆