0

I am trying to acquire 2 decimal digits at the end.
I seen many example on internet but all of them probably formatting from a variable and not directly from textbox.
Also I am confused with one thing.
This works properly :

Dim d1 As Double
txtGAmt.Text = 1500
d1 = txtGAmt.Text
txtGAmt.Text = Format(d1, "0.00")

Here txtGAmt.Text wil be 1500.00

Why this below code don't work as needed?

txtGAmt.Text = 1500
txtGAmt.Text = Format(txtGAmt.Text, "0.00")

This gives me
txtGAmt.Text = 0.00

Following are the things I tried,
txtGAmt.Text = Format("{0:n2}", txtGAmt.Text)
This gives me txtGAmt.Text = 1500 (no change)

Also tried formatcurrency.
It worked good but I don't want the currency symbol and commas in between numbers.

Now my actual problem is do I have to declare variable to add two decimal points everytime in every textbox?

Hitesh Shroff
  • 71
  • 1
  • 1
  • 9
  • If you want to format a number then you need a number. The `Text` of a `TextBox` is a `String`, so attempting to format it as a number makes no sense. You need to convert the `String` to a number first, then format that number as desired. That's why all the examples you see do that: because that's what you do. Also, don't use `Format` in VB.NET. Call `ToString` on the number itself, e.g. `myTextBox.Text = myNumber.ToString("n2")`. If you wanted currency then you'd use "c" as the format specifier. Etc. – jmcilhinney Dec 24 '18 at 13:10
  • As @jmcilhinney said, you need to convert the text to a number. In your second sample, you could use this line instead `TextBox1.Text = Format(Double.Parse(TextBox1.Text), "0.00")` – David Wilson Dec 24 '18 at 13:12
  • Also make sure Option Strict is turned on for all your projects. Have a look at this link to explain a little - https://stackoverflow.com/questions/2454552/what-do-option-strict-and-option-explicit-do – David Wilson Dec 24 '18 at 13:14

1 Answers1

2

The function Format works with numeric values not strings. Try converting your text to a numeric value using Val function, or CDec.

txtGAmt.Text = 1500
txtGAmt.Text = Format(Val(txtGAmt.Text), "0.00")

After posting this answer and checking the reply from @David Wilson, I realized that it might lead to an exception if the content of the text box is numeric value that is out of range if you choose to use CDec instead of Val. So, here is another solution:

txtGAmt.Text = 1500
Dim number As Decimal
If Decimal.TryParse(txtGAmt.Text, number) Then
    txtGAmt.Text = Format(number, "0.00")
Else
    'You can put your own logic here
    txtGAmt.Text = 0
End If
  • The original solution provided will not throw an exception on non-numeric data. It will just produce unexpected results. The `Val` function will ALWAYS return a numeric result. If the input is not numeric, it will simply return 0.0. If the input was, for instance, "123Hello" then `Val` would return 123.0. – jmcilhinney Dec 24 '18 at 13:52
  • Your second solution rather leaves something to be desired because you are parsing the input twice. The `TryParse` method validates and parses the input so you already have the result in `number`, so what point using `Val` after that to parse the same input to get the same output again? – jmcilhinney Dec 24 '18 at 13:54
  • my mistake in my rush i forgot to change the txtGAmt to number – Sulieman Mansouri Dec 24 '18 at 13:55
  • That's more like it. It's still worth noting that the code you have won't compile with `Option Strict On`, which it pretty much should always be. Also, `Format` is a VB6 holdover and you should be calling `ToString` on the number in VB.NET. – jmcilhinney Dec 24 '18 at 14:32
  • Its helped a lot!! – Venu GoPal Jun 21 '23 at 12:13