解析大双值C# [英] Parsing big double value C#

查看:80
本文介绍了解析大双值C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解析非常大的double值将返回错误的值.

Parsing a very big double value is returning wrong value.

例如

Double.Parse("987654321098765432109876543210987.99") 

返回9.8765432109876547E+32,这是错误的.如何处理呢?

returns 9.8765432109876547E+32, which is wrong. How to handle this?

我要在网页中显示以上值,并用逗号分隔.请帮助.

I want to display the above value with comma separated in my web page. Kindly help.

推荐答案

大整数

double的精度有限,因此它将仅存储数字的前15-16位.为了获得更高的精度,请使用System.Numerics.BigInteger-它不支持 非整数,因此您必须自己添加小数点(如果有 固定的小数位数.

Big Integers

double has limited precision, so it will only store the first 15-16 digits of your number. For more precision, use System.Numerics.BigInteger - it doesn't support non-integers though, so you must add the decimal points yourself (if you have a fixed number of decimals).

BigInteger结构

.NET Framework 4.0 +

.NET Framework 4.0+

代表任意大的有符号整数.

Represents an arbitrarily large signed integer.

(来源: http://msdn .microsoft.com/en-us/library/dd268287%28v = vs.99%29.aspx )

相关功能:

解析

将数字的字符串表示形式转换为其BigInteger 等效.

Converts the string representation of a number to its BigInteger equivalent.

命名空间:System.Numerics

Namespace: System.Numerics

程序集:System.Numerics(在System.Numerics.dll中)

Assembly: System.Numerics (in System.Numerics.dll)

public static BigInteger Parse(string value)

(来源: http://msdn .microsoft.com/en-us/library/dd268231%28v = vs.100%29.aspx )

示例:

import System.Numerics.BigInteger;
BigInteger.Parse("98765432109876543210987654321098999").ToString(); // note, no dot

您还需要引用System.Numerics程序集.

You would also need to reference the System.Numerics assembly.

如果数字中需要固定的小数位数,则可以自己实现定点算术和I/O.在此示例中,我将使用两位小数,但可以 容易修改.

If need to have some fixed amount of decimals in the number, you can implement fixed-point arithmetic and I/O yourself. In this example, I'll use two decimal places, but it can be easily modified.

关键是不要使用原始号码,而是原始号码的倍数.例如, 如果您的电话号码是X,则内部表示为X * 100

The key is to not work with the original number, but a multiple of it. For example, if your number is X, it is internally represented as X * 100

加减运算无需修改.让我们考虑数字X,Y和Z, 及其内部表示形式:

Addition and subtraction work without modification. Let's consider numbers X, Y and Z, and their internal representations:

X + Y = Z

X*100 + Y*100 = Z*100

两个方程都是正确的.

乘法和除法需要一些算术运算.让我们修改前面的例子 用于乘法:

Multiplication and division require some arithmetic. Let's modify the previous example for multiplication:

X * Y = Z

(X*100) * (Y*100) = Z*100 <--- WRONG

您需要对方程进行一些修改:

You need to modify the equation somewhat:

X * Y = Z

(X*100) * (Y*100) = Z*100*100

Z * 100 = (X*100) * (Y*100) / 100

因此,您需要将结果除以100,以获得正确的内部表示形式. 使用除法时,您需要将两个操作数分别乘以100.

So you need to divide the result by 100 to get the correct internal representation. With division, you need to multiply both operands by 100 respectively.

如果您的输入始终包含两位十进制数字,则只需删除该十进制 指向获取数字的内部表示.自由格式输入要求 更多工作.

If your input always includes two decimal digits, you can just remove the decimal point to get the internal representation for the number. Free-form input requires more work.

在输出中,您必须在最后两位数字之前插入小数点 得到正确的输入.此外,如果数字小于0,则可能必须 在前面插入零.

In output, you'll have to inject the decimal point before the last two digits to get correct input. Additionally, if the number is less than 0, you may have to insert zeroes in the front.

这篇关于解析大双值C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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