This is my code

Dim dblPrice As Double = 0

        Dim dblTotal As Double = 0
        Dim strTotal As String


        dblPrice = CDbl(txtPrice.Text)
        intQuantity = CInt(txtQuantity.Text)

        dblTotal = (dblPrice * intQuantity)
        strTotal = Format(dblTotal.ToString("N2"))

        If chkDessert.Checked = True Then
            strTotal += 5

        End If

        If chkDrink.Checked = True Then
            strTotal += 2
        End If

        If rbtnDine.Checked = True Then
            strTotal += (strTotal * 0.1)
            MessageBox.Show("Table Number : " & txtNumber.Text & vbCrLf & "Total Bill : RM" & strTotal & "", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)

        End If

        If rbtnTake.Checked = True Then
            strTotal += (strTotal * 0.05)
            MessageBox.Show("Take Away" & vbCrLf & "Total Bill : RM" & strTotal & "", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)

        End If

This code run nicely but the answer not get as two decimal places like what i want.. anybody can help me ?

Dani AI

Generated

The core problem is mixing formatted text and arithmetic. converts the total into a string early, then keeps adding values to that string — VB will try implicit conversions and you get strange results or lose the two-decimal formatting. Do all math using a numeric type, then format only when you display.

Prefer Decimal for money, validate inputs with TryParse, and enable Option Strict to catch accidental implicit conversions. was right to point at numeric formatting for output; ’s use of a formatted display is fine when used only at the end. ’s Val/FormatNumber approach is brittle and can hide parsing errors.

A safe, minimal pattern (calculate numerically, format at output):

' calculate using Decimal and explicit parsing
Dim price As Decimal
Dim qty As Integer
Dim subtotal As Decimal
Dim extras As Decimal = 0D
Dim total As Decimal

If Not Decimal.TryParse(txtPrice.Text, price) Then
    MessageBox.Show("Please enter a valid price.")
    Return
End If
If Not Integer.TryParse(txtQuantity.Text, qty) Then
    MessageBox.Show("Please enter a valid quantity.")
    Return
End If

subtotal = price * qty
If chkDessert.Checked Then extras += 5D
If chkDrink.Checked Then extras += 2D

total = subtotal + extras

If rbtnDine.Checked Then total += total * 0.1D
If rbtnTake.Checked Then total += total * 0.05D

MessageBox.Show("Total Bill : RM" & String.Format("{0:F2}", total))

Quick tips: use Decimal to avoid binary rounding, format only for display (String.Format or ToString with "F2"/"C2"), and decide whether service charges apply before or after extras. If user input may include currency symbols, parse with NumberStyles.Currency and CultureInfo.

Recommended Answers

All 3 Replies

Dim dblTotal As Double = 123123 * 0.05

MessageBox.Show(dblTotal.ToString("N2"))

'i do a simple version for that, it goes this way
'assume that you want to view your answer in text1

text1.txt = formatnumber (Val(text3.text) + Val(text2.text))

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.