JavaMoney: An Emerg­ing Fea­ture for Mone­tary Func­tions in Bank­ing and Fi­nance

JSR 354, which is also termed as JavaMoney, pro­vides a set of money and cur­rency APIs for Java. These APIs pro­vide sup­port for stan­dard ISO-4217 and cus­tom cur­ren­cies. They also help to bridge the gap in the rep­re­sen­ta­tion of a mone­tary amount. JSR 354 pr

OpenSource For You - - Contents - By: Magesh Kasthuri The au­thor is a se­nior dis­tin­guished mem­ber of the tech­ni­cal staff at Wipro. He is an en­ter­prise ar­chi­tect in BFSI. He has au­thored a se­ries of ar­ti­cles on Docker based DevOps in OSFY ear­lier, and can be reached at magesh.kasthuri@wipr

Money is a very com­mon con­cept, used for most ap­pli­ca­tion im­ple­men­ta­tions across dif­fer­ent do­mains. But when it comes to Java based de­vel­op­ment, there is no ded­i­cated sup­port for cur­ren­cies or mone­tary arith­metic li­braries and APIs. The ex­ist­ing java. util.Cur­rency class is strictly a struc­ture used for rep­re­sent­ing ISO-4217 stan­dard cur­ren­cies, and doesn’t pro­vide flex­i­ble or stan­dard­ised cal­cu­la­tion and con­ver­sion fa­cil­i­ties.

It was in Java 1.4 that java.util.Cur­rency got in­tro­duced with a stan­dard three-let­ter cur­rency no­ta­tion like USD or INR. Later, in the Java 7 up­grade, a sup­port for three-let­ter nu­meric code rep­re­sen­ta­tion like USD=840 was in­tro­duced. Also, there is no stan­dard value type to rep­re­sent a mone­tary amount or cur­rency arith­metic func­tions in the cur­rent ver­sion of Java. Ei­ther the user has to rely on some pop­u­lar third party li­brary (like Joda) or write cus­tomised code that may not be generic to global con­ven­tions.

JavaMoney sup­ports cur­rency arith­metic across dif­fer­ent cur­ren­cies, as well as for­eign cur­rency ex­change. Mone­tary val­ues are a key fea­ture of many ap­pli­ca­tions, mainly in bank­ing, fi­nan­cial ser­vices, in­sur­ance and other such sim­i­lar do­mains.

What is money?

Money is a sim­ple rep­re­sen­ta­tion of an amount (value) and the cur­rency (unit). For ex­am­ple, with US$ 100, ‘100’ is the amount and US$ is the unit/cur­rency rep­re­sent­ing this amount.

How do we rep­re­sent money so far

Till now, ap­pli­ca­tion de­vel­op­ment in Java ei­ther used a cur­rency in­ter­face, or used ‘dou­ble’ or ‘BigDec­i­mal’ for money rep­re­sen­ta­tion. But this has a side ef­fect when the float­ing point is a bi­nary ap­prox­i­ma­tion of a dec­i­mal. Though the ap­prox­i­ma­tion is small dec­i­mal, this has its own mean­ing when it comes to the cal­cu­la­tion for money. This kind of con­ven­tion is good when the logic han­dles the value by round­ing the float­ing point and doesn’t con­sider the small­est frac­tional val­ues.

Though BigDec­i­mal doesn’t cause such a float­ing point ap­prox­i­ma­tion, it is slow in cal­cu­la­tion due to in­ter­nal con­ver­sion and of­ten re­quires round­ing mode to han­dle the val­ues rep­re­sented.

Some­times, we also use ‘long’ to rep­re­sent money, which is a much bet­ter choice than the above two rep­re­sen­ta­tions, but since these rep­re­sen­ta­tions don’t as­so­ciate a unit (cur­rency) with them, they can eas­ily con­fuse unit val­ues— for ex­am­ple, dol­lars or eu­ros with cent val­ues.

Us­ing third party li­braries

We now have many so­phis­ti­cated li­braries from third par­ties that can help in bridg­ing the gap left by money rep­re­sen­ta­tion in our ap­pli­ca­tion pro­grams. These also help in ex­tend­ing the fea­tures to en­able cus­tom de­vel­op­ment.

One of the pop­u­lar rep­re­sen­ta­tions is Jo­daMoney, which is a sim­ple wrap­per on BigDec­i­mal and pro­vides con­ven­tional fea­tures for money re­lated arith­metic func­tions.

An­other rep­re­sen­ta­tion in­cludes JS­cience, which fo­cuses more on units, and Eclipse UoMo, a pro­posed open source ini­tia­tive un­der the Eclipse Tech­nol­ogy Project. It is in­tended to sup­port unit and mea­sure­ment APIs (by im­ple­ment­ing APIs

with strong unit typ­ing), and fi­nan­cial APIs based on gen­eral unit and mea­sure­ment im­ple­men­ta­tion.

Com­po­nents of the JavaMoney project

This JSR fo­cuses on defin­ing the in­ter­faces and classes to be used for cur­ren­cies and mone­tary amounts. Gen­er­ally, the fol­low­ing ar­eas are fo­cused upon.

Core: This has the data classes and in­ter­face rep­re­sent­ing cur­ren­cies and mone­tary amounts.

Con­ver­sion: This deals with ex­change rates be­tween cur­ren­cies, and pro­vides the API/SPI to per­form the con­ver­sion of mone­tary amounts from one cur­rency to an­other.

For­mat: This de­fines APIs/SPIs for print for­mat­ting, and the pars­ing of cur­ren­cies and mone­tary amounts, pro­vid­ing sup­port for com­plex us­age sce­nar­ios. Ex­ten­sions: These add ex­tended func­tion­al­ity like cur­rency ser­vices, cur­rency map­ping, re­gions and va­lid­ity ser­vices (his­toric data API).

Some of the key fa­cil­i­ties of mone­tary func­tions are ex­plained be­low.


This is an im­mutable money class and rep­re­sents cur­rency and BigDec­i­mal amounts with any num­ber of dec­i­mal places. An ex­am­ple is shown be­low:

BigMoney amount = BigMoney.parse(“EUR 1.20567”); amount = amount.mul­ti­pliedBy(2); // EUR 2.41134 amount = amount.plusMa­jor(3); // EUR 5.41134 amount = amount.plusMi­nor(5); // EUR 5.46134 bool­ean neg­a­tive = amount.isNega­tive(); // false amount = amount.rounded(4, Round­ingMode.UP);

String str = amount.toString(); // “EUR 5.4614”


This is a re­place­ment of the ex­ist­ing cur­rency class and al­lows ap­pli­ca­tions to con­trol cur­rency data. It also in­cludes three­digit nu­meric ISO code as shown be­low:

Cur­ren­cyUnit cur = Cur­ren­cyUnit.of(“GBP”); int dp = cur.getDec­i­malPlaces(); // 2 String code = cur.getCur­ren­cyCode(); // “GBP” int ncode = cur.getNumer­icCode(); //

String str = cur.toString(); // “GBP”

Cur­rency for­mat­ting

Print­ing cur­rency val­ues and units along with parsed data is also pro­vided with a flex­i­ble builder, like Joda-Time and JSR 310. For ex­am­ple:

MoneyFor­mat­terBuilder b = new MoneyFor­mat­terBuilder(); b.ap­pendCur­ren­cyCode().ap­pendLit­eral(“: “).ap­pendA­mount(

MoneyA­moun­tStyle.ASCII_DECIMAL_POINT_GROUP3_COMMA); MoneyFor­mat­ter f = b.toFor­mat­ter(); String str = f.print(money); // eg “GBP: 1,234.56”

Gen­er­ally, the scope of the JSR is to tar­get all gen­eral ap­pli­ca­tion types, such as: eCom­merce

Bank­ing and fi­nance


In­sur­ance and pen­sion

Mo­bile and em­bed­ded pay­ment

To sum­marise, the scope of JavaMoney can be de­fined as fol­lows:

Pro­vides ab­strac­tions for cur­rency and money suit­able for all tar­get ap­pli­ca­tions.

En­hances avail­able cur­ren­cies and adds sup­port for nonISO cur­ren­cies as well.

Sup­ports com­plex cal­cu­la­tion rules, in­clud­ing cal­cu­la­tion pre­ci­sion and dis­play pre­ci­sion.

Sup­ports re­gional round­ing rules.

Of­fers ba­sic sup­port for for­eign ex­change, ex­tend­able for more com­plex us­age sce­nar­ios.

A plain text rep­re­sen­ta­tion of money.

Print­ing/pars­ing to the string.

Sup­port for plu­ral­i­sa­tion.

Pro­vides APIs for his­toric ac­cess of cur­ren­cies and ex­change rates.

De­fines a flex­i­ble query API to sup­port com­plex us­age sce­nar­ios for ex­change rates and cur­ren­cies.

Stores a value in mul­ti­ple cur­ren­cies (a sin­gle value type that ef­fec­tively maps the cur­rency to the amount, where the amounts are all no­tion­ally the same by some form of cur­rency con­ver­sion).

Large ex­change rate sys­tem (as in a sys­tem suit­able for trad­ing, such as bid/ask).

Nev­er­the­less, there are some lim­i­ta­tions on this front:

It is not planned to tar­get low la­tency such as al­go­rith­mic trad­ing ap­pli­ca­tions, though we should try to keep per­for­mance in mind to ac­com­mo­date such ap­pli­ca­tions. Non-dec­i­mal cur­ren­cies.

JavaMoney is in­cluded with Java 9, with back­ward com­pat­i­bil­ity as well as a plug­gable li­brary. It is be­ing de­vel­oped un­der the stan­dard JSR Spec Li­cense and Apache 2.0 Li­cense for RI (Ref­er­ence Im­ple­men­ta­tion) and TCK (Tech­nol­ogy Com­pat­i­bil­ity Kit). These two mod­ules will be de­vel­oped and dis­trib­uted as stand­alone mod­ules and RI, and bun­dled with the Java 8 SE dis­tri­bu­tion.

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.