Separate a label caption using a period as the separator?
Hi there! I am working on a prog to teach kids about money (dollars and cents). I have a lbl called TotalLBL
Is there a way to separate the caption, and put the left side (dollars) into DollarLBL and right of the decennial will be CentsLBL.
Any help would be greatly appreciated!
Re: Separate a label caption using a period as the separator?
Pretty basic stuff:
1) Use InStr() to find the decimal, then Left & Mid to separate
2) Use Split()
3) Use math if the value is in a numeric data type:
Dollars = Int(Money)
Cents= CLng((Money - Int(Money)) * 100)
Re: Separate a label caption using a period as the separator?
Thank you!
So I got this set up so it takes the left side (dollar side) off from the decimal.
The only thing I want to fix is, it takes off the decimal too.
Example
'Now get the decimal cut off and get the dollar amount
Dim iPos2 As Integer
iPos2 = InStr(Checkout.TotalLBL.Caption, ".")
If iPos2 Then
Checkout.DollarsLBL.Caption = Left$(Checkout.TotalLBL.Caption, iPos2)
Else
' no . , what to do now?
End If
in this case if the decimal was 2.75, the code would put 2. in the DollarLBL. I would like to just have the number with no decimal after it. Did I miss something? : )
EDIT
If I put - 1 after the iPos2, that so far seems to take care of the decimal, leaving me with only the dollar number, but are there any problems that could come from doing that?
Re: Separate a label caption using a period as the separator?
No problems except if the region settings do not use a decimal for the cents. Some languages use a comma instead.
Re: Separate a label caption using a period as the separator?
What if I put an if statement to search the caption for a comma and replace it with a period before separating it?
I know in the states and possibly other places they use commas to denote the thousands, but it my prob we will be lucky to go over $10.
Re: Separate a label caption using a period as the separator?
I think the best answer is not to use a string value, rather use a Currency variable for money. You can display the currency value in the label using the Format() function.
Re: Separate a label caption using a period as the separator?
Ok that sounds great!
TotalLBL.Caption = Format$(TotalLBL.caption, "Currency")
Would that always use a period for a decimal or would that be dependent of what country they are from?
Re: Separate a label caption using a period as the separator?
Dependent on the regional settings. You can use math to separate the whole number from the fraction. You could also use the FormatCurrency() function. It will also display the correct symbol for money based on the regional settings and has a couple more options
Re: Separate a label caption using a period as the separator?
I will sleep easier if you change your naming conventions -
lblTotal
lblDollar
lblCents