Display Float as String with at Least 1 Decimal Place

前端 未结 4 1679
野的像风
野的像风 2020-11-30 12:30

I want to display a float as a string while making sure to display at least one decimal place. If there are more decimals I would like those displayed.

For example:

4条回答
  •  伪装坚强ぢ
    2020-11-30 12:42

    float fNumber = 1.2345; // Your number
    string sNumber = fNumber.ToString(); // Convert it to a string
    If ((sNumber.Contains(".") == false) && (sNumber.Contains(",") == false)) // Check if it's got a point or a comma in it...
    {
        sNumber += ".0"; // ... and if not, it's an integer, so we'll add it ourselves.
    }
    

提交回复
热议问题