简体   繁体   English

Convert.ChangeType搞砸了返回值

[英]Convert.ChangeType messing up return value

We have a validation in place for years in our code base which was doing just fine until now. 我们在代码库中进行了多年验证,直到现在为止还算不错。 The issue started happening when values up to four decimal places started coming into the picture. 当图片中的小数点后四位开始出现问题时,就开始出现此问题。 To visualize review the following line of code; 要可视化,请查看以下代码行;

//upto 3 decimal, returns just fine
//temp is equal to 1000.003
var temp = System.Convert.ChangeType("1000.003", TypeCode.Single);

//Test for values upto 4 decimals
//Iteration:1 
//Supplied 1000.0001 Return 1000.00012
var temp1 = System.Convert.ChangeType("1000.0001", TypeCode.Single);

//Iteration:2
//Supplied 1000.0004 Return 1000.00043
var temp2 = System.Convert.ChangeType("1000.0004", TypeCode.Single);

//Iteration:3
//Supplied 1000.0007 Return 1000.00067
var temp3 = System.Convert.ChangeType("1000.0007", TypeCode.Single);

Why is it acting this way? 为什么这样做呢? And another thing is if I change the TypeCode to double then precision is saved, why? 还有另一件事是,如果我将TypeCode更改为double那么可以保存精度,为什么?

From MSDN : MSDN

A Single value has up to 7 decimal digits of precision, although a maximum of 9 digits is maintained internally. 尽管内部最多可保留9位数字,但“单个”值最多具有7位小数位数的精度。

You're running into cases where the number you pass in can't be represented exactly as a Single , so it gives you the closest Single value it can. 您遇到的情况是,您传入的数字不能完全表示为Single ,因此它可以为您提供最接近的Single值。 If the decimal representation must be preserved exactly then Decimal is a more appropriate type. 如果必须精确保留十进制表示形式,则Decimal是更合适的类型。

Note that the fact that there are four digits after the decimal is irrelevant. 注意,小数点后有四位数字是无关紧要的。 You'd have the same issue with numbers like 100,000,010 您可能会遇到与100,000,010数字相同的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM