Float formatting in .NET

27 12 2005

I’ve used this before, but today I wanted to string format a double (or float), with max. precision of one, but if the whole number was a real, just show the number, without any digits behind the decimal separator.

score.ToString("F2")
-> will show “1,75″, but also “1,00″
String.Format("{0:#.#}", score)
-> will show “1,75″, and “1″ (element 0 in the list is converted)

…with “score” being a variable of type Double, with value 1,75 and 1. Code snippet is in VB.NET.


Actions

Information

Leave a comment