Remove decimal point from a decimal number

前端 未结 7 2198
佛祖请我去吃肉
佛祖请我去吃肉 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 02:06

    A simple solution would be:

    while((int)n != n) n *= 10;
    

    Multiplying the number by 10 moves the decimal point 1 place to the right. You need to repeat this multiplication until there are no more numbers on the right side. To detect if there are more numbers on the right, you simply cast it to int, which drops the decimal part.

提交回复
热议问题