从十进制数字中删除小数点 [英] Remove decimal point from a decimal number

查看:82
本文介绍了从十进制数字中删除小数点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从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:

  • My decimal number is 2353.61 I want 235361 as the result.
  • My decimal number is 196.06 I want 19606 as the result.

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.

decimal d = 2353.61M;
int count = BitConverter.GetBytes(decimal.GetBits(d)[3])[2];
d *= Convert.ToDecimal(Math.Pow(10, count));

Using this answer to get the number of decimals.

这篇关于从十进制数字中删除小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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