4 Replies - 328 Views - Last Post: 02 February 2013 - 03:51 PM Rate Topic: -----

#1 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

setting a default place for decimals

Posted 01 February 2013 - 09:14 PM

i have a textbox, named txtPPrice.text where in you can only input numbers.

i want that textbox to convert integers into decimals onleave.

i.e
i input 150
onleave the output should be 1.50

another i.e
i input 10025
onleave the output should be 100.25

as you can see, the last two digit became the decimals.

here is my first lines of code:
Dim a As Decimal
        a = txtPPrice.Text



and i don't know what would be next.

at first i used this code:
If txtPPrice.Text <> "" Then
txtPPrice.Text = txtPPrice.Text & ".00"
End If



but then, i don't achieved the goals i want.
i hope for some help.
ty

Is This A Good Question/Topic? 0
  • +

Replies To: setting a default place for decimals

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: setting a default place for decimals

Posted 01 February 2013 - 09:45 PM

Format the ToString function for your decimal variable. variable.ToString("N2")
Was This Post Helpful? 0
  • +
  • -

#3 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: setting a default place for decimals

Posted 01 February 2013 - 09:57 PM

You'll probably want to validate the input, so perhaps this would serve the purpose...
    Private Sub TextBox1_LostFocus(sender As Object, e As System.EventArgs) Handles TextBox1.LostFocus
        Dim result As Decimal
        If Decimal.TryParse(TextBox1.Text, result) Then
            TextBox1.Text = CStr(result / 100)
        Else
            TextBox1.Clear()  'or you could put a MessageBox here to inform the user.
        End If
    End Sub



Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is online

  • D.I.C Addict

Reputation: 236
  • View blog
  • Posts: 781
  • Joined: 01-February 13

Re: setting a default place for decimals

Posted 01 February 2013 - 11:34 PM

Hi lar3ry,
You said the textbox only accepts numbers so no need to check if it is a number so if you want to just change the text in the textbox upon leaving the textbox and don`t want to put the text into a variable until later in the program you can try this code. If you only type "7" for 7 cents it will add a zero like ".07" and anything more than 1 number it will place the decimal in the second to last spot like "10025" will be "100.25". This will happen as soon as the textbox looses focus.

    Private Sub txtPPrice_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPPrice.LostFocus
        If txtPPrice.TextLength > 1 Then txtPPrice.Text = txtPPrice.Text.Insert(txtPPrice.TextLength - 2, ".")
        If txtPPrice.TextLength = 1 Then txtPPrice.Text = ".0" & txtPPrice.Text
    End Sub


Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: setting a default place for decimals

Posted 02 February 2013 - 03:51 PM

Consider using a MaskedTextbox instead.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1