十进制数据类型去除尾随零的需要,以显示他们的时候 [英] Decimal data type is stripping trailing zero's when they are needed to display

查看:132
本文介绍了十进制数据类型去除尾随零的需要,以显示他们的时候的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个Web应用程序我的工作(其他开发人员写的)都有一个下降两个零的小数点后的十进制变量。它不下降的后2位数字,如果它们包含许多> 0或的组合。值被从文本文件来。



示例文本值: 261.00



例小数变量(TotalDue)为: 261



在调试时,我悬停在TotalDue(示例代码下面)该值显示为261,当我展开写着261M调试器:

 小数TotalDue = Convert.ToDecimal( InputRow.Substring(260,12)); 



我试图把它作为一个字符串(但最初它仍然读为261,而不是261.00),然后将其转换以各种方式如下。 !没有什么工作



 字符串TotalDue = InputRow.Substring(260,12); 

strTotalDue =的String.Format({0:F2},TotalDue);

strTotalDue =的String.Format({0:N2},TotalDue);

strTotalDue =的String.Format(TotalDue,0.00);

strTotalDue = TotalDue.ToString(G29);

strTotalDue =的String.Format({0:0.00},TotalDue);

strTotalDue = TotalDue.ToString(N2); //用这一个十进制数据类型

我在想什么?不要紧,那里的文本文件数据起源?它开始在Access数据库。



更新:今天(15年12月1日),我意识到,我从来没有为标志的答案,因为我结束了再杀原代码和改写在C#.NET。我将迎来科尔坎贝尔的答案正确的,因为他的言论的(构建小数在为它提供关于输入的精度足够数据的方法。)的是要来是什么原因促使我与解决方案我做了这是操纵输入数据。我这样做的方法 - 只显示的事项(AmtDue)以下的部分。提示输入数据是在261.00的格式(例如AmtDue = 261.00):

 字符串AmtDue = Convert.ToString( AmountDue).Replace(,)。 
串finalstring =(0000000000+ AmtDue).Substring(AmtDue.Length);


解决方案

你的第一个例子是下降了零的原因可能有做你如何创建小数实例。 小数包含影响如何的ToString()工作的比例因子,而这个比例因子是基于如何设置不同的小数构造



此代码:

  VAR D1 = Decimal.Parse(261.00); 
变种D2 =新的十进制数(261.00);
VAR D3 =261.00米;
Console.WriteLine(D1);
Console.WriteLine(D2);
Console.WriteLine(D3);



生成以下结果:

  261.00 
261
261.00

如果你想保留尾随零,构建小数在为它提供关于输入的精度足够数据的方法。



记住,正如其他的答案指出,调试器提供的字符串不一定是一样的),由生成的字符串的ToString(


A web app I'm working on (another dev wrote it) has a decimal variable that is dropping two zero's after the decimal. It does not drop the trailing 2 digits if they contain a number > 0 or a combination of. The value is coming from a text file.

Example text value is: 261.00

Example decimal variable (TotalDue) is: 261

During debug when I hover over the "TotalDue" (in sample code below) the value displays as 261 and when I expand the debugger it reads "261M":

decimal TotalDue = Convert.ToDecimal(InputRow.Substring(260, 12));

I have tried bringing it in as a string (but initially it still reads as "261" instead of 261.00) and then converting it in various ways as follows. Nothing is working!

string TotalDue = InputRow.Substring(260, 12);

strTotalDue = String.Format("{0:F2}", TotalDue);

strTotalDue = String.Format("{0:N2}", TotalDue);

strTotalDue = String.Format(TotalDue, "0.00");

strTotalDue = TotalDue.ToString("G29");  

strTotalDue = String.Format("{0:0.00}", TotalDue);

strTotalDue = TotalDue.ToString("N2");//used this one with decimal data type

What am I missing? Does it matter where the text file data originated? It started in an Access database.

UPDATE: Today (12/1/15) I realized I never marked an answer because I ended up scrapping the original code and rewriting it in C#.net. I will mark Cole Campbell's answer correct because his remarks ("construct the Decimal in a way that provides it with sufficient data regarding the precision of the input.") are what prompted me to come up with the solution I did which was to manipulate the incoming data. I did so in a method - only showing the part that matters (AmtDue) below. Reminder the incoming data was in the format of "261.00" (e.g. AmtDue = 261.00):

string AmtDue = Convert.ToString(AmountDue).Replace(".", "");           
string finalstring =  ("0000000000" + AmtDue).Substring(AmtDue.Length);

解决方案

The reason your first example is dropping the zeroes likely has to do with how you're creating the Decimal instance. Decimal contains a scaling factor which influences how ToString() works, and this scaling factor is set differently based on how the Decimal is constructed.

This code:

var d1 = Decimal.Parse("261.00");
var d2 = new Decimal(261.00);
var d3 = 261.00m;
Console.WriteLine(d1);
Console.WriteLine(d2);
Console.WriteLine(d3);

Produces these results:

261.00
261
261.00

If you want to preserve the trailing zeroes, construct the Decimal in a way that provides it with sufficient data regarding the precision of the input.

Remember that, as noted by other answers, the string provided by the debugger is not necessarily the same as the string produced by ToString().

这篇关于十进制数据类型去除尾随零的需要,以显示他们的时候的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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