C# get digits from float variable

前端 未结 12 1662
南笙
南笙 2020-12-06 16:26

I have a float variable and would like to get only the part after the comma, so if I have 3.14. I would like to get 14 as an integer. How can I do that?

12条回答
  •  伪装坚强ぢ
    2020-12-06 17:17

    Just in case some wants another cheating way for it:

    float x = 5.2f;
    int decimalPart = Math.Round((x - Math.Truncate(x))*100)
    

    where 100 is used shift the decimal part.

提交回复
热议问题