The decimal data type discards the trailing zero when it is needed to display

The web application I'm working on (another author wrote it) has a decimal variable that resets two zeros after the decimal. It does not discard the final 2 digits if they contain a number> 0 or a combination. The value is derived from a text file.

Example text value: 261.00

Example of a decimal variable (TotalDue): 261

During debugging, when you hover over “TotalDue” (in the example below), the value is displayed as 261, and when I expand the debugger, it reads “261M”:

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

I tried to cast it as a string (but initially it still reads as “261” instead of 261.00) and then converts it in various ways as follows. Nothing works!

 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 came from? It started in the Access database.

UPDATE: Today (12/1/15). I realized that I never noted the answer, because in the end I refused the source code and rewrote it in C # .net. I will mark Cole Campbell's correct answer because his remarks ("build the decimal code in such a way as to provide him with sufficient data about the accuracy of the input") prompted me to come up with a solution that I made to manipulate the incoming data. I did this in the method - only showing the part that matters (AmtDue) below. A reminder that the incoming data was in the format "261.00" (for example, AmtDue = 261.00):

 string AmtDue = Convert.ToString(AmountDue).Replace(".", ""); string finalstring = ("0000000000" + AmtDue).Substring(AmtDue.Length); 
+6
source share
4 answers

The reason your first example is removing zeros is probably due to the way you create the Decimal instance. Decimal contains a scaling factor that affects how ToString() works, and this scaling factor is set differently depending on how Decimal built.

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 keep trailing zeros, build Decimal in such a way as to provide it with sufficient input precision data.

Remember that, as noted in other answers, the line provided by the debugger does not necessarily match the line created by ToString() .

+9
source

If you want to use two decimal places, you can use the correct ToString :

 string formatted = TotalDue.ToString("0.00"); 

Demo <

Standard number format strings

(by the way, ToString("D2") does not work)

+13
source

I am sure you can google and most likely came across this link, but here it is for reference:

Paired format string formatting

It seems that you have already tried strTotalDue = String.Format("{0:0.00}", TotalDue); so I’m not sure what is still wrong.

Without additional context, however, we will not know how to solve this problem.

+3
source

The number you see in the debugger is not related to how it is actually displayed. 261M correct is the value "261" stored in decimal format ("M" = "Money" = decimal).

Try entering numeric formatting codes here . "F2" is what you want.

+2
source

All Articles