Parse the decimal character and add filter 0 to the right?

From the XML file, I get decimals in the format:

1.132000 6.000000 

I am currently using Decimal.Parse like this:

 decimal myDecimal = Decimal.Parse(node.Element("myElementName").Value, System.Globalization.CultureInfo.InvariantCulture); 
  • How to print myDecimal in line look below?

     1.132 6 
+14
string decimal c #
Nov 28 '10 at 19:55
source share
5 answers

I don’t think there are standard numbers formatted strings that, I fear, will always skip minor zeros.

You can try to write your own decompilation method, but it can be quite complicated. With the BigInteger class from .NET 4, this would be reasonably doable, but without it (or something like that) it would be very difficult.

EDIT: Well, I think this is what you want:

 using System; using System.Numerics; public static class DecimalExtensions { // Avoiding implicit conversions just for clarity private static readonly BigInteger Ten = new BigInteger(10); private static readonly BigInteger UInt32Mask = new BigInteger(0xffffffffU); public static decimal Normalize(this decimal input) { unchecked { int[] bits = decimal.GetBits(input); BigInteger mantissa = new BigInteger((uint) bits[0]) + (new BigInteger((uint) bits[1]) << 32) + (new BigInteger((uint) bits[2]) << 64); int sign = bits[3] & int.MinValue; int exponent = (bits[3] & 0xff0000) >> 16; // The loop condition here is ugly, because we want // to do both the DivRem part and the exponent check :( while (exponent > 0) { BigInteger remainder; BigInteger divided = BigInteger.DivRem(mantissa, Ten, out remainder); if (remainder != BigInteger.Zero) { break; } exponent--; mantissa = divided; } // Okay, now put it all back together again... bits[3] = (exponent << 16) | sign; // For each 32 bits, convert the bottom 32 bits into a uint (which won't // overflow) and then cast to int (which will respect the bits, which // is what we want) bits[0] = (int) (uint) (mantissa & UInt32Mask); mantissa >>= 32; bits[1] = (int) (uint) (mantissa & UInt32Mask); mantissa >>= 32; bits[2] = (int) (uint) (mantissa & UInt32Mask); return new decimal(bits); } } class Program { static void Main(string[] args) { Check(6.000m); Check(6000m); Check(6m); Check(60.00m); Check(12345.00100m); Check(-100.00m); } static void Check(decimal d) { Console.WriteLine("Before: {0} - after: {1}", d, d.Normalize()); } } } 
+28
Nov 28 '10 at 20:09
source share
β€” -

This will remove all trailing zeros from the decimal, and then you can just use ToString() .

 public static class DecimalExtensions { public static Decimal Normalize(this Decimal value) { return value / 1.000000000000000000000000000000000m; } } 

Or, if you want accurate accuracy, say, 5 decimal places, first Normalize (), and then multiply by 1.00000 m.

+5
Jan 09 '13 at 18:00
source share

This is consistent with the above example, but NOT MORE. (I THINK)

 myDecimal.ToString("#.######") 

What other requirements exist? Are you going to manipulate values ​​and display managed values ​​by that number of decimal places?

An alternative answer includes recursion, for example:

  //use like so: myTextbox.Text = RemoveTrailingZeroes( myDecimal.ToString() ); private string RemoveTrailingZeroes(string input) { if ( input.Contains( "." ) && input.Substring( input.Length - 1 ) == "0" ) { //test the last character == "0" return RemoveTrailingZeroes( input.Substring( 0, input.Length - 2 ) ) //drop the last character and recurse again } return input; //else return the original string } 

And if you need an extension method, then this is an option

  //use like so: myTextbox.Text = myDecimal.ToString().RemoveTrailingZeroes(); private string RemoveTrailingZeroes(this string input) { if ( input.Contains( "." ) && input.Substring( input.Length - 1 ) == "0" ) { //test the last character == "0" return RemoveTrailingZeroes( input.Substring( 0, input.Length - 2 ) ) //drop the last character and recurse again } return input; //else return the original string } 

Added input.Contains( "." ) && for a comment from Jon Skeet, but keep in mind that this will make it incredibly slow. If you know that you will always have a decimal code and there is no such case as myDecimal = 6000; , you can refuse this test, or you can do it in a class and have several private methods based on whether the input contains a decimal number, etc. I was the simplest and "it works" instead of Enterprise FizzBuzz

+4
Nov 28 '10 at 20:09
source share

If you need to do this just for display, then what about using a custom format string?

 // decimal has 28/29 significant digits so 30 "#" symbols should be plenty public static readonly string TrimmedDecimal = "0." + new string('#', 30); // ... decimal x = 1.132000m; Console.WriteLine(x.ToString() + " - " + x.ToString(TrimmedDecimal)); // 1.132 decimal y = 6.000000m; Console.WriteLine(y.ToString() + " - " + y.ToString(TrimmedDecimal)); // 6 
+4
Nov 29 '10 at
source share

How about using toString ("G29") in decimal? it does exactly what you are trying to achieve!

+2
Apr 05 2018-12-12T00:
source share



All Articles