0

First of i'm using Visual Basic 2010 -> Windows Forms Application

I want like, when a value is updated, if it's over 1000 a label should indicate $1.000, if over 10000 => $10.000 and so on... How can i do that ?

    If money > 1000 Then

        lblMoney.Text = ("$" & money.ToString.Substring(0, 1) & "." & 
        _ money.ToString.Substring(1, 3))

    ElseIf money > 10000 Then

        lblMoney.Text = ("$" & money.ToString.Substring(1, 3) & "." & 
        _ money.ToString.Substring(3, 4))

    End If

I tried this but it only works for the first if statement ... i don't know how to use these substrings, i just played with them a little bit and it worked out...

How can i do that ? If possible, without coding to put dots after every new number like 1.000 10.000 100.000 1.000.000 and so on

Bpk7
  • 27
  • 5
  • why don't you make the money a double and then use a string formatter to give you the currency? i.e: money.ToString("c") - this will give you what you want. no need to do string parsing etc... – Ahmed ilyas Apr 04 '14 at 17:37
  • Aghhh, writing buggy code to do what a built-in function does. Please read about number formatting in `VB.NET` before posting please. – John Alexiou Apr 04 '14 at 18:29

1 Answers1

2

Pretty Simple

why don't you make the money a double and then use a string formatter to give you the currency? i.e: money.ToString("c") - this will give you what you want. no need to do string parsing etc...

Dim money as Double = 1000
Console.WriteLine(money.ToString("c"))

this will print (depending on your culture): $1,000

Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72
  • It's usually better to use the Decimal type for currency. There are some reasons given at [What is the difference between Decimal, Float and Double in C#?](http://stackoverflow.com/questions/618535/what-is-the-difference-between-decimal-float-and-double-in-c) (don't worry about it referring to C# instead of VB.NET). – Andrew Morton Apr 04 '14 at 19:10
  • Thanks @AndrewMorton - mine was a quick example and you are right, not sure why I missed that but probably as I have been working with a lot of doubles this week! :) – Ahmed ilyas Apr 04 '14 at 19:11