Group,

I've found code that will format a number found in a TextBox to display it with two decimal places. It looks like this:

    Private Sub txbStateTaxPcnt_Leave(sender As System.Object, e As System.EventArgs) Handles txbStateTaxPcnt.Leave
        stateSlsTax = Convert.ToInt32(txbStateTaxPcnt.Text)
        txbStateTaxPcnt.Text = stateSlsTax.ToString("n2")
    End Sub

This works great when a whole number is entered into the textbox. However when I enter a number with decimal places (ex. '2.25'), I get an error message saying, "Input string was not in a correct format." My assumption is that this "number" is recognized as a string instead of the integer that it really is.

Is there something different that I should use in place of "Convert.ToInt32(txbStateTaxPcnt.Text)"? If so, am I going to need to use a If/Then statement to differenciate the two different inputs?

As always, thanks for the help.

Don

Recommended Answers

All 10 Replies

Just use ToDecimal(). Works everytime.

Eternal Newbie,

I tried the following code:

    Private Sub txbStateTaxPcnt_Leave(sender As System.Object, e As System.EventArgs) Handles txbStateTaxPcnt.Leave
        stateSlsTax = Convert.ToDecimal(txbStateTaxPcnt.Text)
        txbStateTaxPcnt.Text = stateSlsTax.ToString
    End Sub

It worked fine as long as the number in the TextBox was a number with decimals (ex. '2.25). However it did not display decimal places when the whole number was entered. Is it possible that I am using your suggestion incorrectly?

Again, thanks for the help. I look forward to hearing back from you.

Don

I suggest that your 'stateSlsTax' has not been set as Decimal, try this code instead:

        Dim stateSlsTax As Decimal
        stateSlsTax = Convert.ToDecimal(txbStateTaxPcnt.Text)
        txbStateTaxPcnt.Text = stateSlsTax.ToString("n2")

If this one not working corectly, I think you should check if there are any '.Trim()' things around your code.

Hi,
Try this:

txbStateTaxPcnt.Text = stateSlsTax.ToString("#,0.00")

Eternal Newbie,

The '("n2")' fixed the display problem. Thanks.

Hmmm..... I didn't know you could Dim a variable as "Decimal" (yes, I'm a newbie myself).

This prompts a question: In SQL Server Management 2008, I've defined the field where these numbers will be saved as 'Number(4, 2)' with the idea that I'm allocating 2 whole numbers and 2 decimal places (up to 99.99). On the VB side, is it better to define the variable as "Double" or should I define it as 'Decimal'..... or does it matter?

I look forward to your answers.

Don

@Don,
Double show 'e' values when Decimal not (so, not everything is "number"). Someone told me to use Decimal when coding accounting projects and Double for mathmatics ones, not really know the reason.

Enternal Newbie,

I don't follow. When you say that 'Double' shows the 'e' values, what do you mean by 'e' values? What then does 'Decimal' show?

I'm building a sales and inventory program. So, using your understanding, it would make sense for me to use 'Decimal' instead of 'Double'.

Thanks again for the lesson. As a newbie, I'm trying to absorb everything like a sponge. And it's been a lot of fun. I really so appreciate and enjoy this site.

Don

'e' values stands for '10' in Decimal, jusy say: a kind of mathematics symbol. For example: Double says: "e", Decimal says "10"... Btw, I'm also a newbie then learning things also feeling good for me.

Hmmmm.... I'm not the mathematician I thought I was. However I still don't know I follow. You're using a single quote vs. a double quote.

For what it's worth, in reviewing my data entry into my database, 'ToDecimal' does format the whole number (ex. 7) as 7.00, which is what I wanted it to do as I had formatted my SQL column as 'number(4, 2).

Thanks again for the help!

Don

try this..

    Textbox1.Text = Format(Csng(Textbox1.Text),"#,##0.00")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.