Double string with required decimal point

It's probably dumb, but it's hard for me. I need to convert / format double to string with a required decimal point.

 1 => 1.0 0.2423423 => 0.2423423 0.1 => 0.1 1234 => 1234.0 

Basically, I want to print all the decimal numbers, but also make sure that the rounded values ​​have a redundant .0 too. I am sure there is an easy way to achieve this.

+7
source share
6 answers

There is no built-in method for adding a mandatory .0 to the end of integers using the .ToString() method, since existing formats will be truncated or rounded depending on the number of decimal places specified.

My suggestion is to simply execute my own implementation using the extension method

 public static String ToDecmialString(this double source) { if ((source % 1) == 0) return source.ToString("f1"); else return source.ToString(); } 

And use:

 double d1 = 1; double d2 = 0.2423423; double d3 = 0.1; double d4 = 1234; Console.WriteLine(d1.ToDecimalString()); Console.WriteLine(d2.ToDecimalString()); Console.WriteLine(d3.ToDecimalString()); Console.WriteLine(d4.ToDecimalString()); 

The results of this conclusion:

 1.0 0.2423423 0.1 1234.0 
+4
source

Use double.ToString("N1") :

 double d1 = 1d; double d2 = 0.2423423d; double d3 = 0.1d; double d4 = 1234d; Console.WriteLine(d1.ToString("N1")); Console.WriteLine(d2.ToString("N1")); Console.WriteLine(d3.ToString("N1")); Console.WriteLine(d4.ToString("N1")); 

Demo

Standard number format strings

Numeric ("N") format specifier

Update

(1.234) .ToString ("N1") creates 1.2 and in addition to removing extra decimal digits, it also adds a thousands separator

Well, maybe you need to implement a custom NumberFormatInfo object, which you can get from the current CultureInfo and use in double.ToString :

 var culture = CultureInfo.CurrentCulture; var customNfi = (NumberFormatInfo)culture.NumberFormat.Clone(); customNfi.NumberDecimalDigits = 1; customNfi.NumberGroupSeparator = ""; Console.WriteLine(d1.ToString(customNfi)); 

Please note that you need to clone it, since it is the default by default.

Demo

+7
source

You can do something like this: if a number has no decimal points, you can format its output to provide a single decimal number 0, and if it has decimals, just use ToString ();

 double a1 = 1; double a2 = 0.2423423; string result = string.Empty; if(a1 - Math.Floor(a1) >0.0) result = a1.ToString(); else result = a1.ToString("F1"); if (a2 - Math.Floor(a2) > 0.0) result = a2.ToString(); else result = a2.ToString("F1"); 

When you use "F" as formatting, the output will not contain a thousands separator, and the number that follows it indicates the number of decimal places.

+3
source

Double provides the ToString() method, where you can pass an IFormatProvider object that indicates how you want to convert double .

In addition, it should display the final 0 at all costs.

 value = 16034.125E21; // Display value using the invariant culture. Console.WriteLine(value.ToString(CultureInfo.InvariantCulture)); // Display value using the en-GB culture. Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB"))); // Display value using the de-DE culture. Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE"))); // This example displays the following output to the console: // -16325.62015 // -16325.62015 // -16325,62015 // 1.6034125E+25 // 1.6034125E+25 // 1,6034125E+25 

Here is the documentation from MSDN.

0
source

Use ToString ("0.0 #############################").

He works. I found it in two copies of your question, the decimal formatting of ToString, which gives at least 1 digits, with no upper limit

0
source

You can apply to the string and then add “.0” if there was no decimal point specified

 string sValue=doubleValue.ToString(); if(!sValue.Contains('.')) sValue+=".0"; 

EDIT: As mentioned in the comments of '.' may not be the decimal separator in the current culture. See this article for the actual delimiter if you want to make your own code for this case.

-one
source

All Articles