[英]How to Find the number zero in decimal part where add no value to the number and remove that part
I am using the following code to convert cm to the meter.我正在使用以下代码将厘米转换为米。
public static double? ConvertCmToM(double? cm)
{
return cm.Value * 0.01;
}
When I input the number 8.8 output giving as当我输入数字8.8 output 时
0.08800000000000001m
0.08800000000000001米
But I want to stop in the index where zero adds no value in the decimal part.但我想在索引中停止,其中零在小数部分不增加任何值。 In this case I want to display the value as
在这种情况下,我想将值显示为
0.088m
0.088米
This is already done in major converter websites.这已经在主要的转换器网站上完成了。 when you type the cm to m converter on google those sites will appear.
当你在谷歌上输入 cm 到 m 转换器时,这些网站就会出现。 How do they do it?
他们是怎么做到的呢?
I took the same sample and put in their sites and this is how they show.我拿了同样的样本并放入他们的网站,这就是他们的展示方式。
> 0.088m
I cannot blindly substring the value after converting to a string as the zero part will appear in 5th or 6th element.我不能盲目地 substring 转换为字符串后的值,因为零部分将出现在第 5 个或第 6 个元素中。 That also handled in those sites.
这也在那些网站上处理过。
This is a double data type.这是双精度数据类型。 the letter "m" concat at the last minute.
最后一分钟连接字母“m”。 How to acehive this?
如何实现这个?
I would use decimal instead of double then:我会使用 decimal 而不是 double 然后:
public static decimal? ConvertCmToM(decimal? cm)
{
if(cm == null) return null;
return Decimal.Multiply(cm.Value, 0.01m);
}
static void Main()
{
Console.Write(ConvertCmToM(8.8m)); // 0.088
}
decimal vs double? 十进制与双? - Which one should I use and when?
- 我应该在什么时候使用哪一个?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.