0

I have some doubles and I need to print them (in reality I use them in WPF UI) but the resulting string has too many characters (or digits) and I need to show all the significant ones. Removing trailing zeros would help:

"1.00000000" => "1", "2.50000000" => "2.5", "205.01280000" => "205.0128"

I know I can round() the numbers to a given number of digits but it decreases precision... Is there a function or any way to remove trailing zeros?

Edit: my english was terrible at the time I asked this question. I didn't know the word "trailing" was a thing and so didn't know there was already a question about this here.

Thanks to all the people who took the time to answer anyway!

Iconejey
  • 15
  • 6

4 Answers4

1

I suspect you don't actually want to round the numbers, as the target number of decimals is variable. Also given you have " around your numbers leads me to believe you are dealing with strings.

You could accomplish what you describe using String.Trim(Char[])

E.g.

string[] vals = {"1.00000000", "2.50000000", "205.01280000"};
char[] trimChars = {'.', '0'};

foreach (var v in vals) {
    Console.WriteLine(v.Trim(trimChars));
}
Chris Pickford
  • 8,642
  • 5
  • 42
  • 73
  • 4
    `1.00000000` will result in `1.` with a trailing `.` – Tim Schmelter Feb 05 '19 at 16:17
  • Updated to trim trailing . – Chris Pickford Feb 05 '19 at 16:21
  • 1
    i don't like treating numbers as strings. This should really be parsed to decimal first. Maybe i'd upvote it if you'd at least use `NumberFormatInfo.CurrentInfo.NumberDecimalSeparator` instead of the hardcoded `.`. Also use `TrimEnd` not `Trim` – Tim Schmelter Feb 05 '19 at 16:23
  • Might be a good idea asking what OP requires if input string is "0.0000" - I would guess "0", rather than an empty string. Another special case reason to avoid treating numeric as strings. – PaulF Feb 05 '19 at 16:45
0
string source = "2.4200";
string output = double.Parse(source).ToString();

Credits: here

Mikev
  • 2,012
  • 1
  • 15
  • 27
  • This would depend on the values not exceeding the precision of double, otherwise rounding errors would occur. – PaulF Feb 05 '19 at 16:49
0

If you don't need a constant precision, but you want to remove unimportant zeros, you can use string.Trim method(). It has also a variant for end of string only.

yourString.TrimEnd('0');

It will do exactly what you need, using the TrimEnd will protect numbers like 0.1 to be trimmed from both sides.

"1.00000000" => "1", "2.50000000" => "2.5", "205.01280000" => "205.0128"

EDIT: Trim also comas to avoid sth like "1.".

yourString.TrimEnd(new []{'0', '.'});
Ayron
  • 46
  • 1
  • 4
  • Might be a good idea asking what OP requires if input string is "0.0000" - I would guess "0", rather than an empty string. – PaulF Feb 05 '19 at 16:46
-1

try this simple solution:

string number_string = "1.6540000000";
double number = double.Parse(number_string);
Console.WriteLine(number);

In that way you can keep using the number_string as a double (performing arithmetical actions on it, etc).

Daniel Reyhanian
  • 579
  • 4
  • 26