Click here to Skip to main content
15,886,565 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I Have a problem to sum decimal.
I Have textbox8, textbox9 and textbox10.
Textbox8 = 500,50
Textbox9 = 21,10
For Textbox10 i have 605 and not 606,1055

VB
Private Sub TextBox8_TextChanged(sender As Object, e As EventArgs) Handles TextBox8.TextChanged
    ' Calcular Precio Final Con IVA
    Dim Sin_IVA, IVA, Total_IVA As Decimal
    Sin_IVA = Val(TextBox8.Text)
    IVA = Val(TextBox9.Text)

    Total_IVA = ((Val(Sin_IVA) * Val(IVA)) / 100) + Val(Sin_IVA)
    TextBox10.Text = Total_IVA.ToString
    ' Calcular Precio Final Con IVA

    ' Calcular dif Precio Final Con IVA
    TextBox11.Text = Val(Total_IVA) - Val(Sin_IVA)
    ' Calcular dif Precio Final Con IVA


End Sub


What is wrong ? Thanks for helping.

What I have tried:

I try double but i use "," and not "."
Posted
Updated 5-Nov-19 23:11pm
Comments
Herman<T>.Instance 6-Nov-19 3:37am    
What is Exception message?
Member 12919322 6-Nov-19 3:42am    
I don't have error message. The sum is wrong !

Your problem is culture.
Your computer probably has US culture or something, which uses a dot as a decimal separator and not a comma.
A comma is a thousand separator.

The solution is to provide a culture that uses comma as a decimal separator, such as nl-NL (Netherlands).
This can be done with TryParse (the result is passed as reference).
VB
Dim Sin_IVA, IVA, Total_IVA As Decimal
Decimal.TryParse(TextBox8.Text, Globalization.NumberStyles.Float, New Globalization.CultureInfo("nl-NL"), Sin_IVA)
Decimal.TryParse(TextBox9.Text, Globalization.NumberStyles.Float, New Globalization.CultureInfo("nl-NL"), IVA)

' Use Sin_IVA and IVA here.
Of course this will only work with comma's and never with dots, no matter what you set on your computer.

I'd recommend never using Val again and go for the Convert.To* or *.(Try)Parse methods.
 
Share this answer
 
This seems like a code which has been copied from VB to VB.NET...
First of all, you should use proper parsing functions; then, once you have obtained proper decimal values, use these values and do not repeat the parsing process.
VB.NET
Private Sub TextBox8_TextChanged(sender As Object, e As EventArgs) Handles TextBox8.TextChanged
   ' Calcular Precio Final Con IVA
   Dim Sin_IVA, IVA, Total_IVA As Decimal
   If Not Decimal.TryParse(TextBox8.Text, Sin_IVA) Then
      '' Handle here the case where Sin_IVA is not a proper decimal value
   End
   If Not Decimal.TryParse(TextBox9.Text, IVA) Then
      '' Handle here the case where IVA is not a proper decimal value
   End
   '' If you get here, both values have been validated, and you do not need to repeat the parsing process.
   Total_IVA = Sin_IVA * (1D + IVA / 100D)
   Dim stringRepresentation As String = Total_IVA.ToString("#0.0000")
End Sub
 
Share this answer
 
Ok thanks for reply but i think somethings is wrong with mysql, i have the follow error.
Data truncated for colum 'Precio_Sin_Iva' at row 1.
Maybe put all code help better :

Imports MySql.Data.MySqlClient
Imports System.Configuration
Imports System.Resources
Imports System.Decimal
Public Class CosteMaquinaria
    Dim MysqlConn As MySqlConnection
    Dim COMMAND As MySqlCommand
    Dim READER As MySqlDataReader
    Dim a As New OpenFileDialog
    Dim dr As MySqlDataReader
    Private Sub CosteMaquinaria_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox9.Text = "21,00"

        MysqlConn = New MySqlConnection(ConfigurationManager.ConnectionStrings("test").ToString)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MysqlConn.Close()
        MysqlConn.Open()

        MysqlConn = New MySqlConnection(ConfigurationManager.ConnectionStrings("test").ToString)

        Me.Cursor = Cursors.WaitCursor
        Dim SDA As New MySqlDataAdapter
        Dim dbDataSet As New DataTable
        Dim bSource As New BindingSource
        Try
            MysqlConn.Close()
            MysqlConn.Open()
            Dim Query As String
            Query = "select Numero_Interno,Matricula,Marca,Numero_Serie,Modelo,Tipo_Maquinaria from xCantera.Flota"
            COMMAND = New MySqlCommand(Query, MysqlConn)
            SDA.SelectCommand = COMMAND
            SDA.Fill(dbDataSet)
            bSource.DataSource = dbDataSet
            DataGridView1.DataSource = bSource
            DataGridView1.DataSource = bSource
            DataGridView1.Columns("Tipo_Maquinaria").Visible = False
            SDA.Update(dbDataSet)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            MysqlConn.Dispose()
        End Try
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.RowIndex >= 0 Then
            Dim row As DataGridViewRow
            row = Me.DataGridView1.Rows(e.RowIndex)
            TextBox1.Text = row.Cells("Numero_Interno").Value.ToString
            TextBox2.Text = row.Cells("Matricula").Value.ToString
            TextBox3.Text = row.Cells("Marca").Value.ToString
            TextBox4.Text = row.Cells("Numero_Serie").Value.ToString
            TextBox5.Text = row.Cells("Modelo").Value.ToString
            TextBox6.Text = row.Cells("Tipo_Maquinaria").Value.ToString
        End If
        'Seleccion Combobox1
        Try
            MysqlConn.Open()
            Dim Query As String
            Query = "select Categorias from xCantera.CostesMaquinaria_SubGrupos where Tipo_Maquinaria = '" & TextBox6.Text & "' ORDER BY Categorias ASC"
            COMMAND = New MySqlCommand(Query, MysqlConn)
            READER = COMMAND.ExecuteReader

            'Reinicio Combo
            ComboBox1.Items.Clear()
            'TextBox1.Clear()

            While READER.Read
                ComboBox1.Items.Add(READER.GetString("Categorias"))
            End While

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            MysqlConn.Dispose()
        End Try
        'Seleccion Combobox1

        If ComboBox1.Items.Count > 0 Then
            ComboBox1.SelectedIndex = 0    ' The first item has index 0 '
        End If

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

    End Sub

    Private Sub TextBox6_TextChanged(sender As Object, e As EventArgs) Handles TextBox6.TextChanged

    End Sub

    Private Sub TextBox10_TextChanged(sender As Object, e As EventArgs) Handles TextBox10.TextChanged

    End Sub

    Private Sub TextBox8_TextChanged(sender As Object, e As EventArgs) Handles TextBox8.TextChanged
        ' Calcular Precio Final Con IVA
        Dim Sin_IVA, IVA, Total_IVA As Decimal

        Sin_IVA = TextBox8.Text
        IVA = TextBox9.Text

        Total_IVA = ((Sin_IVA * IVA) / 100) + Sin_IVA
        TextBox10.Text = Total_IVA.ToString

        TextBox11.Text = Total_IVA - Sin_IVA

    End Sub

    Private Sub TextBox9_TextChanged(sender As Object, e As EventArgs) Handles TextBox9.TextChanged
        '' Calcular Precio Final Con IVA
        'Dim Sin_IVA, IVA, Total_IVA As Decimal
        'Sin_IVA = Val(TextBox8.Text)
        'IVA = Val(TextBox9.Text)

        'Total_IVA = ((Val(Sin_IVA) * Val(IVA)) / 100) + Val(Sin_IVA)
        'TextBox10.Text = Total_IVA.ToString
        '' Calcular Precio Final Con IVA

        '' Calcular dif Precio Final Con IVA
        'TextBox11.Text = Val(Total_IVA) - Val(Sin_IVA)
        '' Calcular dif Precio Final Con IVA
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'Insertar en Base de Datos
        Me.Cursor = Cursors.WaitCursor
        'Dim MysqlConn As MySqlConnection
        Dim Sin_IVA, Con_IVA, Diff_IVA As Decimal
        Sin_IVA = Val(TextBox8.Text)
        Con_IVA = Val(TextBox10.Text)
        Diff_IVA = Val(TextBox11.Text)
        Dim COMMAND As New MySqlCommand
        Dim READER As MySqlDataReader
        Dim a As New OpenFileDialog

        Try
            MysqlConn.Open()
            COMMAND.Connection = MysqlConn
            COMMAND.CommandText = "INSERT INTO  CostesMaquinaria (Numero_Interno, Grupo_Gastos, Numero_Factura, Precio_Sin_Iva, IVA, Precio_Con_Iva, Diff_IVA) 
            Values(@Value1,@Value2,@Value3,@Value4,@Value5,@Value6,@Value7)"
            COMMAND.Parameters.AddWithValue("@Value1", TextBox1.Text)
            COMMAND.Parameters.AddWithValue("@Value2", ComboBox1.Text)
            COMMAND.Parameters.AddWithValue("@Value3", TextBox7.Text)
            COMMAND.Parameters.AddWithValue("@Value4", TextBox8.Text)
            COMMAND.Parameters.AddWithValue("@Value5", TextBox9.Text)
            COMMAND.Parameters.AddWithValue("@Value6", TextBox10.Text)
            COMMAND.Parameters.AddWithValue("@Value7", TextBox11.Text)
            READER = COMMAND.ExecuteReader
            MsgBox("Data Save !", MsgBoxStyle.Information)
            MysqlConn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            MysqlConn.Dispose()
        End Try
        Me.Cursor = Cursors.Default

    End Sub
End Class


My datatable are the follow :

CREATE TABLE IF NOT EXISTS `CostesMaquinaria` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `Numero_Interno` varchar(255) DEFAULT NULL,
  `Grupo_Gastos` varchar(255) DEFAULT NULL,
  `Numero_Factura` varchar(255) DEFAULT NULL,
  `Precio_Sin_Iva` decimal(10,3) DEFAULT NULL,
  `IVA` decimal(10,3) DEFAULT NULL,
  `Precio_Con_Iva` decimal(10,3) DEFAULT NULL,
  `Diff_IVA` decimal(10,3) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900