Currency!! Converting a String value to Decimal for calculation

fr0s1y

Member
Joined
May 16, 2011
Messages
14
Programming Experience
1-3
Ok,

I've searched for the solution for this however 'answers' i've found have perhaps been a little more than I've needed...

basically i have 2 textboxes on my form, 'textbox1' is populated by a value from a database, say.. 200.00 on form load. 'textbox2' needs to accept a users value (say 50.00) and then add/subtract that from the value in 'textbox1' when the form's refreshed/submitted/whatever

whats the syntax involved in this process, i've seen efforts involving parse, 'Cint' etc but need to check there isn't some ridiculously easy way of doing this......as previous tasks in vb.net have shown me!!
 
this is as far as i've got:

VB.NET:
 Dim curBalance As Decimal
        Dim usrInput As Integer
        Dim calculation As Decimal

        curBalance = Decimal.Parse(currentBalance.ToString)

        usrInput = Integer.Parse(amountAddedText.Text)

        If amountAddedText.Text = " " Then

            calculation = curBalance + usrInput

        End If

        curBalance = calculation.ToString()

lol still dont know how to embed code!!! i'm rubbish at forum speak :(:(
 
Ok,
basically i have 2 textboxes on my form, 'textbox1' is populated by a value .. say.. 200.00. 'textbox2' needs to accept a users value (say 50.00) and then add/subtract that from the value in 'textbox1' when the form's refreshed/submitted/whatever

I am a bit confused with the code you posted, amountAdded.Text is the text from the textbox right? you said the value is something like 50.00, so why is it that you are converting it to an integer in your code?

VB.NET:
usrInput = Integer.Parse(amountAddedText.Text)


Looks like you got it though, heres another way you can approach it if i am understanding what your doing correctly? I just outputted the result to a messagebox

        Dim loadedValue As Decimal = 200D
        Dim inputtedValue As Decimal = 0D

        If Decimal.TryParse(txtInput.Text, inputtedValue) AndAlso (inputtedValue <> 0) Then
            Dim calculation As Decimal = inputtedValue + loadedValue

            MsgBox(calculation.ToString("00.00"))

        Else
            MsgBox("Invalid Input")
        End If


You mentioned Cint, you can use Cdec also if that is what your looking for?

If you know that the inputted value is always going to be a valid decimal or integer value then something like this could be sufficient?


        
        Dim calculation As Decimal = CDec(txtInput.Text) + loadedValue
        MsgBox(calculation.ToString("00.00"))



you can even do something like this if you wanted to, i dont really like this approach but you could do it...? i would prefer to just use the tryparse method.

        MsgBox((CDec(txtInput.Text) + loadedValue).ToString("00.00"))


alternatively there are other methods like the decimal.parse you used, CType(txtInput.Text, Decimal), and so forth...

[edit]the .Tostring("00.00") i used is just to format the string, if this was a currency you can just use "C", there are a bunch of other formats out there, or you could just make a custom format like i shown. If you need to round the decimal you can use some methods in the Math class, like Round, or even Ceiling or Floor if that is what your looking to do.

[edit] also you can use a numericUpDown control, this way you dont even need to convert the value, set the decimal places property to 2 or something, then just retrieve the value.
'something like this..
MsgBox((NumericUpDown1.Value + loadedValue).ToString("00.00"))

'elsewhere..
'this is generated code from VS for the num control
    Private Sub InitializeComponent()
        Me.NumericUpDown1 = New System.Windows.Forms.NumericUpDown()
        CType(Me.NumericUpDown1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'NumericUpDown1
        '
        Me.NumericUpDown1.DecimalPlaces = 2
        Me.NumericUpDown1.Location = New System.Drawing.Point(12, 12)
        Me.NumericUpDown1.Maximum = New Decimal(New Integer() {1000, 0, 0, 0})
        Me.NumericUpDown1.Name = "NumericUpDown1"
        Me.NumericUpDown1.Size = New System.Drawing.Size(120, 20)
        Me.NumericUpDown1.TabIndex = 3
        Me.NumericUpDown1.Value = New Decimal(New Integer() {200, 0, 0, 0})
    End Sub
    Friend WithEvents NumericUpDown1 As System.Windows.Forms.NumericUpDown
 
Last edited:
Dim calculation As Decimal = CDec(txtInput.Text) + loadedValue
MsgBox(calculation.ToString("00.00"))

how can one know so much?!?! this is the one!!! thanks man u digged me out there!!
 
No problem, glad it helped :)
 
Back
Top