从十进制数字中删除小数点 [英] Remove decimal point from a decimal number
问题描述
我正在尝试从C#中的十进制数字中删除小数点。
例如:
- 我的十进制数字是
2353.61
我想要的结果是235361
。 - 我的十进制数字是
196.06
我想要的结果是19606
。
我该怎么做?
我将简单地获取小数位数并乘以10的正确乘方数。可能不是真正的问题,但这也将更快,使用更少的内存,然后将其转换为字符串拆分/重组,然后将其转换回a双。
decimal d = 2353.61M;
int count = BitConverter.GetBytes(decimal.GetBits(d)[3])[2];
d * = Convert.ToDecimal(Math.Pow(10,count));
使用 I am trying to remove just the decimal point from a decimal number in C#. For example: How can I do that? I would simply get the number of decimals and multiply it by the correct power of 10. Probably not really a concern but this also would be faster and use less memory then casting it to a string splitting / recombining it, then casting it back to a double. This also works for any number of decimal places. Using this answer to get the number of decimals. 这篇关于从十进制数字中删除小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
小数位数
2353.61
I want 235361
as the result.196.06
I want 19606
as the result.decimal d = 2353.61M;
int count = BitConverter.GetBytes(decimal.GetBits(d)[3])[2];
d *= Convert.ToDecimal(Math.Pow(10, count));