Room of a pleinolijf

Ask yourself this: how do I want to be remembered ?

Float formatting in .NET

Leave a comment

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.

Advertisements

Author: pleinolijf

father | volleyballer | software engineer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s