Automatically Set Decimal Points?

dholt

New member
Joined
Aug 27, 2012
Messages
3
Programming Experience
1-3
Hi there,

I am programming a Cash Register program in VB.NET (Visual Basic 2010) and I have got to the point to input the price of an item. In our old, manual till, we could type in the price without the comma or point, in other words for 2.99 we would type "299" and then the department number. Is there any way i could put this function in to my program? If yes, then please send code!

Thanks
 
You could simply divide the input number by 100, providing the user enters 2 digits for the cents. The result would be a type Double or Decimal.
 
You could simply divide the input number by 100, providing the user enters 2 digits for the cents. The result would be a type Double or Decimal.

Thanks, would there be any way to do it so that if the user typed 1 it would registeer as one cent? (And type 100 for a pound/euro)

Thanks so much already,

Daniel
 
A single digit divided by 100 would still give you the correct number.
But if they enter a dollar amount, it must be followed by 2 digits or it won't work correctly.
I suggest displaying the actual number after the input and having the user make any corrections if it's wrong, or continue if it's correct.
 
Last edited:
Thanks, it has worked but now I have problems with adding up: If I press subtotal, and I have, as in this Example added up:

2,5
5,5
0,23
0,98

When I ask for the subtotal, I get:

9,209999

How can I get it to only give me decimals that have only two digits at the end (x.xx)

Thanks,

Daniel
 
Round out the number to 2 decimal places using the Math.Round() method. It has 2 arguments -- the first is the number to round, and the 2nd is the number of places after the decimal. for example:

longnum= 9.909999
shortnum = Math.Round(longnum, 2)
 
Round out the number to 2 decimal places using the Math.Round() method. It has 2 arguments -- the first is the number to round, and the 2nd is the number of places after the decimal. for example:

longnum= 9.909999
shortnum = Math.Round(longnum, 2)

Better to avoid the issue in the first place rather than try to fix it. That looks like a floating-point issue, which would imply that you're using type Double. It is recommended to use type Decimal for financial and other high-precision calculations. It is slower to work with than Double, which is why Double is preferred where high precision is not required, but Decimal specifically avoids situations like this. When using literals, use the D suffix to force a value to type Decimal, e.g.
Dim text = TextBox1.Text
Dim number As Decimal

If Decimal.TryParse(text, number) Then
    If Not text.Contains(Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator) Then
        number = number / 100D
    End If

    TextBox1.Text = number.ToString("n2")
Else
    MessageBox.Show("Invalid input")
End If
 
Back
Top