19

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: 1 should be displayed as 1.0 1.2345 should display as 1.2345

Can someone help me with the format string?

KrisTrip
  • 4,943
  • 12
  • 55
  • 74

4 Answers4

32

Use ToString(".0###########") with as much # as decimals you want.

Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
  • 12
    Per the [IEEE 754 Standard](http://en.wikipedia.org/wiki/IEEE_754-2008), with `float` you can use 6 `#`s after the `.0` as you get ~7 decimal digits of precision. For `double` you can use 15 `#`s as you get ~16 decimal digits of precision. – user7116 Nov 07 '11 at 16:08
  • @Six: thanks for the information. I will use it for myself too! – Ignacio Soler Garcia Nov 07 '11 at 18:23
  • @User7116 This is misleading. For values really close to 0 the approximate range of a single is ±1.5 x 10−45 which is far more than 7 digit. You should check your precision based on your current value [IEEE_754 Precisions](https://en.wikipedia.org/wiki/IEEE_754#Basic_and_interchange_formats). – TermWay Sep 28 '19 at 17:40
13

This solution is similar to what other are saying, but I prefer to use string.Format. For example:

float myFloat1 = 1.4646573654;
float myFloat2 = 5;
Console.WriteLine(string.Format("Number 1 : {0:0.00##}", myFloat1));
Console.WriteLine(string.Format("Number 2 : {0:0.00##}", myFloat2));

// Newer Syntax
Console.WriteLine($"{myFloat1:0.00##}";
Console.WriteLine($"{myFloat2:0.00##}";

This would produce :

Number 1 : 1.4646
Number 2 : 5.00
Number 1 : 1.4646
Number 2 : 5.00
Tipx
  • 7,367
  • 4
  • 37
  • 59
3

Try this:

doubleNumber.ToString("0.0###");

And, for your reference (double ToString method): http://msdn.microsoft.com/en-us/library/kfsatb94.aspx

Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68
Michael Todd
  • 16,679
  • 4
  • 49
  • 69
  • That forces everything to have 1 decimal place. So in this case 1.2345 would display as 1.2 – KrisTrip Nov 07 '11 at 16:02
  • I was actually looking for a way to do a min of 1 decimal and no max – KrisTrip Nov 07 '11 at 16:05
  • No it doesn't: `(1.23456789).ToString("0.0#######") = "1.23456789"` – Deanna Nov 07 '11 at 16:07
  • 1
    @KrisTrip: How many decimals are you expecting? Only a finite amount can be represented on a standard computer. (And how many are actually going to be useful to the viewer?) – Deanna Nov 07 '11 at 16:08
  • 1
    @Deanna - Originally the formatting was "0.0" which did force 1 decimal. It was edited after that comment. – KrisTrip Nov 07 '11 at 16:14
0
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.
}
RobinJ
  • 5,022
  • 7
  • 32
  • 61
  • That breaks internationalization (works great for en-US, but not for others) – KrisTrip Nov 07 '11 at 16:04
  • this might not work depending on localisation settings. (if thats a concern) – Matt Nov 07 '11 at 16:05
  • Yeah, I just thought of the fact that it wouldn't work on my own computer because of the localisation :p Fixed the if clause though. Problem is that it's a bit dodgy to get it to detect wheter it should add a point or a comma. – RobinJ Nov 07 '11 at 16:05
  • 1
    What you're really looking for @RobinJ is the [`NumberDecimalSeparator`](http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numberdecimalseparator.aspx). However, this is still a brittle choice. – user7116 Nov 07 '11 at 18:27