How to can I strip zeros and decimal points off of decimal strings?

You can also use decimal's ToString with a parameter.

Up vote 3 down vote favorite 2 share g+ share fb share tw.

The following code currently outputs: 12.1 12.100 12.1000 12.00 12 12.0000 How can I change it so it outputs: 12.1 12.1 12.1 12 12 12 Math. Round seems to be the thing, but it makes me define how many decimal places I want, but I want them to be variable as above. If there is no mathematical way to do it, I'll just strip the zeros and decimal points off the right side of the strings, but would think there is a math way to handle this.

Using System; using System.Collections. Generic; namespace Test8834234 { public class Program { static void Main(string args) { List decimalsAsStrings = new List { "12.1", "12.100", "12.1000", "12.00", "12", "12.0000" }; foreach (var decimalAsString in decimalsAsStrings) { decimal dec = decimal. Parse(decimalAsString); Console.

WriteLine(dec); } Console.ReadLine(); } } } c# decimal rounding link|improve this question asked 12.10007 at 12:40Edward Tanguay21.9k42228508 80% accept rate.

You can also use decimal's ToString with a parameter: string s = dec. ToString("0. #"); Note: You may have an internationalization issue with your code.

The way you have coded it, it will use the culture info from the user's computer, which may have something other than . For the decimal separator. This might cause your program to give incorrect results for users that have .

For thousands separator. If you want to guarantee that the parse method will always behave the same, you could use CultureInfo. InvariantCulture.

If you do actually want to parse the string according to the user's culture settings, what you are doing is fine.

Use: Console. WriteLine("{0:0. ####}", dec); Learn more about numeric format strings from here...

This string format should make your day: "0. #############################". Keep in mind that decimals can have at most 29 significant digits though.

Examples:? (1000000.00000000000050000000000m). ToString("0.

#############################") -> 1000000.0000000000005? (1000000.00000000000050000000001m). ToString("0.

#############################") -> 1000000.0000000000005? (1000000.0000000000005000000001m). ToString("0.

#############################") -> 1000000.0000000000005000000001? (9223372036854775807.0000000001m). ToString("0.

#############################") -> 9223372036854775807? (9223372036854775807.000000001m). ToString("0.

#############################") -> 9223372036854775807.000000001.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions