在C#中解析字符串小数店精度?有哪些影响? [英] Decimal stores precision from parsed string in C#? What are the implications?

查看:160
本文介绍了在C#中解析字符串小数店精度?有哪些影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IRC上的谈话,有人指出了以下内容:

During a conversation on IRC, someone pointed out the following:

decimal.Parse("1.0000").ToString() // 1.0000
decimal.Parse("1.00").ToString() // 1.00

如何/为何小数键入保留精度(或者,更确切地说,显著数字)这样吗?我的印象是,这两个值相等,没有明显的下

How/why does the decimal type retain precision (or, rather, significant figures) like this? I was under the impression that the two values are equal, not distinct.

这也引发了更多的问题:

This also raises further questions:


  • 如何在数学运算决定的显著的数字是多少?

  • 请问的显著数字位数序列化过程中得到保留?

  • 是否当前区域性影响的方式这是处理?

推荐答案

在数学运算是如何决定的显著数字位数

这是在的 ECMA-334 C#4规格11.1.7第112页

This is specified in the ECMA-334 C# 4 specification 11.1.7 p.112

一个十进制数表示为十的幂缩放一个整数。对于绝对值小于1.0m的
小数,它的值精确到
至少第28位小数。对于绝对值
大于或等于1.0m的decimal,它的值精确到至少28
位。

A decimal is represented as an integer scaled by a power of ten. For decimals with an absolute value less than 1.0m, the value is exact to at least the 28th decimal place. For decimals with an absolute value greater than or equal to 1.0m, the value is exact to at least 28 digits.

是否的显著数字位数得到序列化过程中保留下来?

是的,它确实与seriallization的价值和其精度不不改变

Yes it does, with seriallization the value and its precision does not change

[Serializable]
public class Foo
{
    public decimal Value;
}

class Program
{
    static void Main(string[] args)
    {
        decimal d1 = decimal.Parse("1.0000");
        decimal d2 = decimal.Parse("1.00");

        Debug.Assert(d1 ==d2);

        var foo1 = new Foo() {Value = d1};
        var foo2 = new Foo() {Value = d2};

        IFormatter formatter = new BinaryFormatter();
        Stream stream = new FileStream("data.bin", FileMode.Create, FileAccess.Write, FileShare.None);
        formatter.Serialize(stream, d1);
        stream.Close();

        formatter = new BinaryFormatter();
        stream = new FileStream("data.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
        decimal deserializedD1 = (decimal)formatter.Deserialize(stream);
        stream.Close();

        Debug.Assert(d1 == deserializedD1);

        Console.WriteLine(d1); //1.0000
        Console.WriteLine(d2); //1.00
        Console.WriteLine(deserializedD1); //1.0000

        Console.Read();
    }
}



是否当前区域性影响的方式这处理?

目前的文化只影响如何小数可以从一个字符串进行分析,例如,它可以处理的作为一个区域性特定小数点符号或货币符号,你应该提供的,如£123.4500。文化不改变对象在内部存储的方式,它不影响其精度。

The current culture affects only how a decimal can be parsed from a string, for example it can handle '.' or ',' as a culture-specific decimal point symbol or the currency symbol, should you provide it, e.g. "£123.4500". Culture does not change the way an object is stored internally and it does not affect its precision.

在内部,小数有一个尾数,指数和一个符号,所以什么都没有空格。

Internally, decimal has a mantissa, an exponent and a sign, so no space for anything else.

这篇关于在C#中解析字符串小数店精度?有哪些影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆