字符串包含从十进制转换时尾随零 [英] String contains trailing zeroes when converted from decimal

查看:147
本文介绍了字符串包含从十进制转换时尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到一个程序我正在写一个不寻常的怪癖,而我试图找出是否有人知道的原因。请注意,固定这个问题是很容易的。我只是想不通为什么它是摆在首位发生。

I've run into an unusual quirk in a program I'm writing, and I was trying to figure out if anyone knew the cause. Note that fixing the issue is easy enough. I just can't figure out why it is happening in the first place.

我在写的 VB.NET 的一个WinForms程序时显示的数据的一个子集。它包含几个标签,显示数值(标签的。文本属性正在从十进制值直接分配)。这些数字是由一个DLL我的写C#的返回。该DLL调用最初返回问题的值的web服务。它返回一个作为一个字符串,其它为十进制(我没有在web服务的任何控制,我只是使用它)。该DLL分配这些对一个对象的属性(两者均为小数),那么对象返回给调用该DLL在WinForm程序返回。显然,有很多从WebService消耗等数据,但没有其他操作发生的可能修改这些属性。

I have a WinForms program written in VB.NET that is displaying a subset of data. It contains a few labels that show numeric values (the .Text property of the labels are being assigned directly from the Decimal values). These numbers are being returned by a DLL I wrote in C#. The DLL calls a webservice which initially returns the values in question. It returns one as a string, the other as a decimal (I don't have any control over the webservice, I just consume it). The DLL assigns these to properties on an object (both of which are decimals) then returns that object back to the WinForm program that called the DLL. Obviously, there's a lot of other data being consumed from the webservice, but no other operations are happening which could modify these properties.

所以,简单的版本是:


  • WinForm的请求一个新的从DLL。

  • DLL创建对象

  • DLL调用Web服务,它返回 SomeOtherFoo

//Both Foo.Bar1 and Foo.Bar2 are decimals
Foo.Bar1 = decimal.Parse(SomeOtherFoo.Bar1); //SomeOtherFoo.Bar1 is a string equal to "2.9000"
Foo.Bar2 = SomeOtherFoo.Bar2; //SomeOtherFoo.Bar2 is a decimal equal to 2.9D


  • DLL返回美孚到WinForm的。

  • DLL returns Foo to WinForm.

    WinForm.lblMockLabelName1.Text = Foo.Bar1 //Inspecting Foo.Bar1 indicates my value is 2.9D
    WinForm.lblMockLabelName2.Text = Foo.Bar2 //Inspecting Foo.Bar2 also indicates I'm 2.9D
    


  • 那么,有什么怪癖?

    WinForm.lblMockLabelName1.Text显示为2.9000,而WinForm.lblMockLabelname2.Text显示为2.9

    WinForm.lblMockLabelName1.Text displays as "2.9000", whereas WinForm.lblMockLabelname2.Text displays as "2.9".

    现在,我知道C#和VB的一切表明,这是最初解析成十进制的字符串格式应该有叫上同小数点以后Decimal.ToString的()操作的结果无关。我期望 decimal.Parse(someDecimalString)的ToString()将返回字符串没有任何尾随零。一切我在网上找的,似乎证实了这个(有无数的堆栈溢出的问题,要求完全相反......如何保持从最初的解析格式)。

    Now, everything I know about C# and VB indicates that the format of the string which was initially parsed into the decimal should have no bearing on the outcome of a later decimal.ToString() operation called on the same decimal. I would expect that decimal.Parse(someDecimalString).ToString() would return the string without any trailing zeroes. Everything I find online seems to corroborate this (there are countless Stack Overflow questions asking exactly the opposite...how to keep the formatting from the initial parsing).

    目前,我刚刚删除从被解析的初始字符串,这已隐藏怪癖尾随零。不过,我很想知道为什么它摆在首位发生。

    At the moment, I've just removed the trailing zeroes from the initial string that gets parsed, which has hidden the quirk. However, I'd love to know why it happens in the first place.

    推荐答案

    这是因为比例因子也的 preserves任何尾随零的一个十进制数。尾随零不影响算术或比较运算的十进制数的值。但是,尾随零可能会被如果施加适当的格式字符串的ToString方法显示出来。

    It's because the scaling factor also preserves any trailing zeros in a Decimal number. Trailing zeros do not affect the value of a Decimal number in arithmetic or comparison operations. However, trailing zeros might be revealed by the ToString method if an appropriate format string is applied.

    这篇关于字符串包含从十进制转换时尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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