Home > database >  How to Save Decimal Without Rounding in VB.NET
How to Save Decimal Without Rounding in VB.NET

Time:01-26

I have a program that save amount that is inserted by user

here is the function to save the amount inserted by user

Protected Sub btn_Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Submit.Click

    If Not Page.IsValid Then
        Exit Sub
    End If

    Dim objNewAmount As New Amount

    If Not objCurrAmount Is Nothing Then
        objNewAmount.AmountID = objCurrAmount.AmountID
        isUpdate = True
    End If

    objNewAmount.AmountName = txtAmountName.Text
    Dim amount As Double = txtAmount.Text
    objNewAmount.Amount= amount

    Dim IsError As Boolean = False
    Try
        objNewAmount.AmountName.UpdateAmount()
    Catch ex As Exception
        lblInfo.Text = ex.Message
        IsError = True
    End Try
    If Not IsError Then
        Response.Redirect("AmountList.aspx", True)
    End If
End Sub

Then when I insert the amount of 2,32 I expected it to save 2,32 but instead it save 232 instead without the separator

Data type for the amount is decimal(18,2) in database and decimal in vb code class

This is my snippet of class of Amount objNewAmount is

Private _Tarif As Decimal = 0
Public Property Tarif() As Decimal
        Get
            Return _Tarif
        End Get
        Set(ByVal value As Decimal)
            _Tarif = value
        End Set
    End Property

Can anyone help me,

Snippet for UpdateAmount()

strSQL.Append(" begin" & vbCrLf)
        strSQL.Append("  " & GetNewShortIDQuery("m_Amount", "AmountID", "JH", "@NewID", 6, 4) & vbCrLf)
        strSQL.Append("  insert into m_Amount" & vbCrLf)
        strSQL.Append("   select @NewID" & vbCrLf)
        strSQL.Append("   , " & ReplaceQuota(AmountName) & vbCrLf)
        strSQL.Append("   , " & ReplaceQuota(Amount, "num") & vbCrLf)
        strSQL.Append("   , " & ReplaceQuota(CreatorID) & vbCrLf)
        strSQL.Append("   , " & ReplaceQuota(CreatorIP) & vbCrLf)
        strSQL.Append("   , " & ReplaceQuota(CreatorDateTime, "datetime") & vbCrLf)
        strSQL.Append("   , null" & vbCrLf)
        strSQL.Append("   , null" & vbCrLf)
        strSQL.Append("   , null;" & vbCrLf)
        strSQL.Append(" end" & vbCrLf)

CodePudding user response:

Thank you all @topsail @Hursey and @jmcilhinney for your help I've resorted to using varchar since if I use decimal when ever I use comma it can't be inserted and if I use point it will be gone instead

So i've decided to change the data type into varchar so it can be inserted to the database and since it doesn't interfere with the calculation

  • Related