# dim money

#### wheretheidivides

##### Active Member
OK, I'm on a roll with these suggestions, but am almost out of them. This one has bugged me sense I was a kid. Why can't there be a money type? That way you can add, subtract, multiple and so forth without any kind of stupid conversions. It could also round. This is long over due. Let the expert programmers do this 1 time so the rest of us don't have to. This is looooong overdue.
========================================
Example:

Dim ABC ("\$", true, 1, ".", 2, 2, 9, true, true) as Money
Dim A, B, C (ABC) as MoneyFormat

A=4100
B=212.4000000656
C=A+B
=====================================
1) "\$" = currency type. Can be any symbol if "", then none shown
2) true = comma placement for every 3 digits left of decimal
3) 1 = min number of places before decimal
4) "." = decimal type. Can be any symbol. if "" then no decimal
5)2 = min number of numbers after the decimal
6)2 = max number of numbers after the decimal
7) 9 = number of places after decimal that it remembers. That way the extra digits are not lost.
8) true = round up or down (false means round down always)
9) true = save all digits after decimal (false means only keep rounded numbers after decimal place)
=====================================
so, you format ABC and B4A does the rest.

So, C is "\$4,312.40"
A.Num is 4100.00
B.Num is 212.4000000656
C.Num is 4312.40
(this allows the full numbers to be saved even after rounding)
===============================
You can use arith functions as well

x = B.Round(2) 'rounds to 2 decimal places
so x is 212.40

X = A.Square
so x is 16,810,000

Last edited:

#### IanMc

##### Well-Known Member
Cool suggestions,

There should also be a secret Superman3/Office Space function hidden in the code.

B4X:
``````Sub stos(remainder as Float) 'Superman Three Office Space
iansAccount = iansAccount + remainder
End Sub``````

:sign0188:

Actually and just for fun it would be interesting to see how people write this as a B4a Sub, I'm sure there will be different takes on it and its interesting to see people's different styles of code, don't worry if your code is pre-historic like mine give it a go, show us what ya got.

Usually what happens is someone like me will write the Sub in spaghetti code then the clever guys will show how it should really be done by taking most of the lines of code and crunching them down to just a few lines.

I think this is an excellent way of showing what can be done with B4a BASIC and can be really good pointers for those of us not that familiar with it yet.

ian

Last edited:

#### Mahares

##### Expert
With the use of the AHlocale lib, I created this code I use:

B4X:
``````Dim A,B, C As Double
A= 4100.00 :B= 212.4000000656
C=A+B
Msgbox(MyCurrencyFormat(C),"")   'returns: \$4,312.40

Sub MyCurrencyFormat(MyNumber As Double) As String
Dim AHL As AHLocale   'need the AHlocale library
AHL.InitializeUS
Dim MyCur As String =AHL.CurrencySymbol
Dim MyFrac As Int =AHL.CurrencyFractionDigits
Return MyCur & NumberFormat2(MyNumber,1,MyFrac,MyFrac,True)
End Sub``````

#### Roger Garstang

##### Well-Known Member
This sounds like a job for a library. You also need to consider other types of rounding. Most learn Away from Zero in school. You also have Towards Zero and Bankers Rounding that rounds to the nearest even number to prevent bias and not have the ability to put the bias in your bank account as your Office Space example. PowerBASIC and C# both use Bankers. I've had to deal with a lot of COBOL/Mainframe apps that rounded really weird too along with placement of the - being all over the place.

#### eelias

##### Active Member
It is nice to have a Currency or Money format native, since MANY apps deal with that and BigDecimals are not really clear in its use. Better as a native variable type.

I could suggest using BCD, very common and easy to implement and perfect for keep money formats. Any other way is fine, just that can be defined and operations be simple like dealing with int values.

Android Question EditBox decimal problem
Replies
1
Views
242
Replies
14
Views
2K
Android Question Double Type & Decimals
Replies
6
Views
2K
Replies
5
Views
6K
Replies
13
Views
14K