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

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

问题描述

我正在开发的一个网络应用程序(另一个开发人员写的)有一个十进制变量,它在小数点后去掉两个零.如果它们包含数字 > 0 或它们的组合,它不会删除尾随的 2 位数字.该值来自文本文件.

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.

示例文本值为:261.00

示例十进制变量 (TotalDue) 是:261

Example decimal variable (TotalDue) is: 261

在调试期间,当我将鼠标悬停在TotalDue"(在下面的示例代码中)上时,该值显示为 261,当我展开调试器时,它会显示为261M":

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));

我尝试将它作为字符串引入(但最初它仍然读作261"而不是 261.00),然后以如下各种方式转换它.什么都不起作用!

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

我错过了什么?文本文件数据的来源是否重要?它始于 Access 数据库.

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

更新: 今天(2015 年 12 月 1 日)我意识到我从未标记过答案,因为我最终废弃了原始代码并在 C#.net 中重写了它.我将把 Cole Campbell 的答案标记为正确,因为他的评论 (以一种为它提供有关输入精度的足够数据的方式构造小数.") 是促使我想出解决方案的原因我这样做是为了操纵传入的数据.我以一种方法这样做 - 只显示下面重要的部分(AmtDue).提醒传入数据的格式为261.00"(例如 AmtDue = 261.00):

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);

推荐答案

您的第一个示例删除零的原因可能与您创建 Decimal 实例的方式有关.Decimal 包含一个缩放因子,它会影响 ToString() 的工作方式,并且此缩放因子的设置取决于 Decimal 的构造方式.

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.

此代码:

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);

产生这些结果:

261.00
261
261.00

如果您想保留尾随零,请以一种方式构造 Decimal,为其提供有关输入精度的足够数据.

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.

请记住,正如其他答案所指出的,调试器提供的字符串不一定与 ToString() 生成的字符串相同.

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天全站免登陆