Show zeros after decimal

  • Thread starter Thread starter Sheldon
  • Start date Start date
S

Sheldon

Hello -

For some reason, I am unable to type 0.000 or 0.100 in code. Everytime I do
it, it's automatically stripped away to 0.0 or 0.1.

Is there a setting or something I need to change?
 
Sheldon said:
Hello -

For some reason, I am unable to type 0.000 or 0.100 in code. Everytime I do
it, it's automatically stripped away to 0.0 or 0.1.

Is there a setting or something I need to change?

0.1 is the same as 0.100

Numbers don't have leading or trailing decimal places. Only strings have.
 
Hello Jack, hello Sheldon,

Dim X As Double = CDbl("0.100")
Dim Result As Boolean = MyFunction(CDbl("0.000"), CDbl("0.100"))

This is not a good method, because it will not work if the decimal
separator is not a period. On my system (German), the decimal separator
is a ",".

The following code

Dim dec As Decimal
dec = CDec("1.00")
MsgBox(dec.ToString())

does not show "1.00", but "100", because in Germany the period is used
only for separating thousands (on English system that would be ",").

The proper way to do it would be:

Dim dec1 As Decimal
dec1 = Convert.ToDecimal("100.00", _
Globalization.CultureInfo.GetCultureInfo("en-us"))
MsgBox(dec1.ToString())

On my system this returns "100,00" as expected.

The question is just if you want to take this effort just to have the
".00" visible in your code. If it's just for you to remind you of those
trailing zeros, why don't you just write
dec = 100 '100.00

Best regards,

Martin
 
Hello -

For some reason, I am unable to type 0.000 or 0.100 in code. Everytime I do
it, it's automatically stripped away to 0.0 or 0.1.

Is there a setting or something I need to change?

Why exactly would you want to do that?
 
Hello -

For some reason, I am unable to type 0.000 or 0.100 in code. Everytime I do
it, it's automatically stripped away to 0.0 or 0.1.

Is there a setting or something I need to change?

I never thought of trying to do this, but the C# editor lets you do
this. Perhaps you really want a decimal type? Even so, without the
option that Jack mentioned, it's not possible to format the code this way.
 
Sheldon said:
Hello -

For some reason, I am unable to type 0.000 or 0.100 in code.
Everytime I do
it, it's automatically stripped away to 0.0 or 0.1.

Is there a setting or something I need to change?

No, although the Framework's Decimal retains trailing zeros, VB is
documented to remove trailing zeros in Decimal literals at compile time
(even if you avoid it in the IDE or don't use the iDE).

So the Decimal.Parse options described in the other posts are your only
option.

(However note in my sample code below that the Const calculation occurs
"after" VB's stripping of zeros.)
--
Regards,
Mark Hurd, B.Sc.(Ma.) (Hons.)

Const b As Decimal = 2.3000D
Dim c As Decimal = 2.3000D
Dim d As Decimal = CDec("2.3000")
Dim e As Decimal = 2.3001D - 0.0001D
Const f As Decimal = 2.3001D - 0.0001D
Console.WriteLine("{0:g}", b)
Console.WriteLine("{0:g}", c)
Console.WriteLine("{0:g}", d)
Console.WriteLine("{0:g}", e)
Console.WriteLine("{0:g}", f)
Console.ReadLine()
' Output is
'2.3
'2.3
'2.3000
'2.3000
'2.3000
 
Back
Top