PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 7, 2022

[FIXED] How to remove trailing zeros when using doubles in strings with C#?

 August 07, 2022     c#, decimal, double, string     No comments   

Issue

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!


Solution

string source = "2.4200";
string output = double.Parse(source).ToString();

Credits: here



Answered By - Mikev
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing