Remove decimal point from a decimal number

前端 未结 7 2134
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-19 01:32

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
7条回答
  •  佛祖请我去吃肉
    2021-01-19 01:44

    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.

提交回复
热议问题