Click here to Skip to main content
14,325,129 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to make my Point of Sales program look more professional. I am storing the prices of products in a database as integers. Then, when I import these product prices into my program, the prices do not look very good.

For example, the price of product A is $2.00. This is what I store in my database in a currency column. Then, I import this into my program, and change a label to display the price:

Label1.Text = "$" & ProductPrice

Where ProductPrice is an integer.
However, instead of the price being displayed as "$2.00", it is displayed as "$2". I came up with this solution that works to sort of solve my problem:

If InStr(ProductPrice.ToString, ".") > 0 Then
     Label1.Text = "$" & ProductPrice & "0"
     Label1.Text = "$" & ProductPrice & ".00"
End If

You might be thinking now that I don't have a problem, but there is a problem with the code above. It works fine if the ProductPrice is "2.5", as it changes it to "$2.50", or if the ProductPrice is "2", as that is changed to "$2.00", but if the price is "2.99", then it will change it to "$2.990".

Now finally, to my question. How can I count the number of digits after the decimal place, so that if there are 2 numbers after the decimal place, like "2.99", then it won't add the zero ($2.99), but if there is only one "2.5", then it will add a zero ($2.50).

Hopefully this makes sense!

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You should really look into String.Format methods.

Label1.Text = ProductPrice.ToString("C")

Standard Numeric Format Strings[^]

The "C" formatter is for Currency, and will format currency according to the CultureInfo on your computer. For example, 2 gets formatted as $2.00 in the US. 2.5 gets formatted as $2.50 (complete with currency symbol). In other countries it has the appropriate symbol and format.
JOFA666 22-Jun-13 22:42pm
Thanks. I obviously like to make stuff harder than it has to be.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100