问题描述
这是我收到的格式小数一个XML文件:
From a XML file I receive decimals on the format:
1.132000
6.000000
目前我使用Decimal.Parse是这样的:
Currently I am using Decimal.Parse like this:
decimal myDecimal = Decimal.Parse(node.Element("myElementName").Value, System.Globalization.CultureInfo.InvariantCulture);
-
如何打印myDecimal串
看起来像下面?How do print myDecimal to stringto look like below ?
1.132 6
推荐答案
我不认为有任何标准的数值格式字符串将始终忽略尾随无意义的零,我害怕。
I don't think there are any standard numeric format strings which will always omit trailing insignificant zeroes, I'm afraid.
您可以尝试写自己的十进制归一化法,但它可能是相当棘手。从.NET 4中的的BigInteger
类这将是合理可行的,但是不(或类似的东西),这将是非常艰难的。
You could try to write your own decimal normalization method, but it could be quite tricky. With the BigInteger
class from .NET 4 it would be reasonably feasible, but without that (or something similar) it would be very hard indeed.
编辑:好的,我想这是你想要什么:
Okay, 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());
}
}
}
这篇关于解析十进制和右侧过滤额外0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!