SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Downloaden Sie, um offline zu lesen
Money and Currency
API
Pavel Rozenblioum, Jayway
@javatomte
Today’s plan
• Why?
• What?
• Code
Why do we need it
float beerAtGreenLionInKrona = 54f;
int beerAtGreenLionInÖre = 54_00;
long beerAtGreenLionInÖre = 54_00;
double beerAtGreenLionInKrona = 54;
BigDecimal beerAtGreenLionInKrona = new BigDecimal(“54”);
Why do we need it
• Money is important in many applications
• No standard type for money or support for
conversions
• Currently formatting is limited
Money and Currency API
• JSR 354
• JEP in the works
• RI Moneta http://javamoney.github.io/
• Main contributors: Anatole Tresch and Werner
Keil
Overview of the API
• javax.money - CurrencyUnit,MonetaryAmount,
MonetaryOperator, factory classes,
exceptions…
• javax.money.conversion - CurrencyConversion,
ExchangeRate
• javax.money.format - formatting
• javax.money.spi - service provider interfaces etc


<dependencies>

<dependency>

<groupId>org.javamoney</groupId>

<artifactId>moneta</artifactId>

<version>1.0-RC3</version>

</dependency>

</dependencies>

Currency
CurrencyUnit sek = MonetaryCurrencies.getCurrency("SEK");

//SEK
Money
MonetaryAmount hundredSek = MonetaryAmounts

.getDefaultAmountFactory()

.setNumber(100.03523452435)

.setCurrency(sek)

.create();

// SEK 100.03523452435
Money
Money alsoHundredSek = Money

.of(100.03523452435, sek);
// SEK 100.03523452435
FastMoney
FastMoney fastHundredSek = FastMoney

.of(100.03523452435, sek);
// java.lang.ArithmeticException:
100.03523452435 can not be represented by this
class, scale > 5
FastMoney
FastMoney fastHundredSek = FastMoney

.of(100.03523, sek);

//SEK 100.03523
Money + FastMoney
MonetaryAmount twoHundredSek =

hundredSek.add(fastHundredSek);

// SEK 200.07046452435
// class Money
FastMoney + Money
MonetaryAmount twoHundredSek =

fastHundredSek.add(hundredSek);

//java.lang.ArithmeticException: Parameter
exceeds maximal scale: 5
FastMoney + Money
Money roundedMoney = Money.of(100.03523, sek);

MonetaryAmount twoHundredSek =

fastHundredSek.add(roundedMoney);

//SEK 200.07046
Mixing Currencies
CurrencyUnit dkk = MonetaryCurrencies

.getCurrency("DKK");

Money hundredDKK = Money.of(100, dkk);

hundredSek.add(hundredDKK);

//javax.money.MonetaryException: Currency
mismatch: SEK/DKK
Converting Currencies
CurrencyConversion dkkConversion =

MonetaryConversions.getConversion("DKK");

MonetaryAmount hundredSekInDKK =

hundredSek.with(dkkConversion);


//DKK
81.285852598919388135620537993604757933150536
Formatting support
String britishHundredSek = MonetaryFormats

.getAmountFormat(Locale.UK)

.format(hundredSek);

//SEK100.04
String swedishHundredSek = MonetaryFormats

.getAmountFormat(new Locale("sv"))

.format(hundredSek);

//SEK 100,04
Custom formats
// Creating a custom MonetaryAmountFormat

AmountFormatQuery formatQuery = AmountFormatQueryBuilder

.of(new Locale("sv"))

.set(CurrencyStyle.NAME)

.set(AmountFormatParams.PATTERN, "Money! ##,##.00 ¤")

.set(AmountFormatParams.GROUPING_SIZES, new int[]{4})

.set(AmountFormatParams.GROUPING_GROUPING_SEPARATORS, new char[]{'?'})

.build();

MonetaryAmountFormat customFormat =

MonetaryFormats.getAmountFormat(

formatQuery);

print(customFormat.format(Money.of(1_000_000, sek)));

Money! 100?0000,00 svensk krona
Custom Currencies
CurrencyUnitBuilder.of("FOO", "")

.setDefaultFractionDigits(2)

.setNumericCode(1_000_000)

.build(true);

MonetaryCurrencies.getCurrency("FOO")
Play around!
Thank you!
@javatomte
pavel@rozenblioum.se

Weitere ähnliche Inhalte

Ähnlich wie Moneyandcurrency2

Modern web application development with java ee 7
Modern web application development with java ee 7Modern web application development with java ee 7
Modern web application development with java ee 7Shekhar Gulati
 
Browserify All the things
Browserify All the thingsBrowserify All the things
Browserify All the thingsBen Drucker
 
APIs with Bounded Contexts: Modelling Apis with Domain-Driven Design
APIs with Bounded Contexts: Modelling Apis with Domain-Driven DesignAPIs with Bounded Contexts: Modelling Apis with Domain-Driven Design
APIs with Bounded Contexts: Modelling Apis with Domain-Driven DesignJosé Haro Peralta
 
SV.CO’s iterative product development
SV.CO’s iterative product developmentSV.CO’s iterative product development
SV.CO’s iterative product developmenthari
 
Experience Session - Hari
Experience Session - HariExperience Session - Hari
Experience Session - HariSV.CO
 
The Monitoring and Metic aspects of Eclipse MicroProfile
The Monitoring and Metic aspects of Eclipse MicroProfileThe Monitoring and Metic aspects of Eclipse MicroProfile
The Monitoring and Metic aspects of Eclipse MicroProfileHeiko Rupp
 
Simplifying Use of Hive with the Hive Query Tool
Simplifying Use of Hive with the Hive Query ToolSimplifying Use of Hive with the Hive Query Tool
Simplifying Use of Hive with the Hive Query ToolDataWorks Summit
 
2.0 Client Libraries & Using the Java Client by Noah Crowley, Developer Advoc...
2.0 Client Libraries & Using the Java Client by Noah Crowley, Developer Advoc...2.0 Client Libraries & Using the Java Client by Noah Crowley, Developer Advoc...
2.0 Client Libraries & Using the Java Client by Noah Crowley, Developer Advoc...InfluxData
 
Using the Java Client Library by Noah Crowley, DevRel | InfluxData
Using the Java Client Library by Noah Crowley, DevRel | InfluxDataUsing the Java Client Library by Noah Crowley, DevRel | InfluxData
Using the Java Client Library by Noah Crowley, DevRel | InfluxDataInfluxData
 
APIs : Mapping the way
APIs : Mapping the wayAPIs : Mapping the way
APIs : Mapping the wayWSO2
 
Lessons learned on the Azure API Stewardship Journey.pptx
Lessons learned on the Azure API Stewardship Journey.pptxLessons learned on the Azure API Stewardship Journey.pptx
Lessons learned on the Azure API Stewardship Journey.pptxapidays
 
Chris Mathias Presents Advanced API Design Considerations at LA CTO Forum
Chris Mathias Presents Advanced API Design Considerations at LA CTO ForumChris Mathias Presents Advanced API Design Considerations at LA CTO Forum
Chris Mathias Presents Advanced API Design Considerations at LA CTO ForumChris Mathias
 
Construindo aplicações com HTML5, WebSockets, e Java EE 7
Construindo aplicações com HTML5, WebSockets, e Java EE 7Construindo aplicações com HTML5, WebSockets, e Java EE 7
Construindo aplicações com HTML5, WebSockets, e Java EE 7Bruno Borges
 
Introduction to JSR 354 (Currency and Money) by Anatole Tresch, Werner Keil
Introduction to JSR 354 (Currency and Money) by Anatole Tresch, Werner KeilIntroduction to JSR 354 (Currency and Money) by Anatole Tresch, Werner Keil
Introduction to JSR 354 (Currency and Money) by Anatole Tresch, Werner KeilCodemotion
 
Punta Dreamin 17 Generic Apex and Tooling Api
Punta Dreamin 17 Generic Apex and Tooling ApiPunta Dreamin 17 Generic Apex and Tooling Api
Punta Dreamin 17 Generic Apex and Tooling ApiAdam Olshansky
 
Scaling with swagger
Scaling with swaggerScaling with swagger
Scaling with swaggerTony Tam
 
JavaOne 2014 BOF4241 What's Next for JSF?
JavaOne 2014 BOF4241 What's Next for JSF?JavaOne 2014 BOF4241 What's Next for JSF?
JavaOne 2014 BOF4241 What's Next for JSF?Edward Burns
 
Middleware in Golang: InVision's Rye
Middleware in Golang: InVision's RyeMiddleware in Golang: InVision's Rye
Middleware in Golang: InVision's RyeCale Hoopes
 

Ähnlich wie Moneyandcurrency2 (20)

Modern web application development with java ee 7
Modern web application development with java ee 7Modern web application development with java ee 7
Modern web application development with java ee 7
 
Browserify All the things
Browserify All the thingsBrowserify All the things
Browserify All the things
 
APIs with Bounded Contexts: Modelling Apis with Domain-Driven Design
APIs with Bounded Contexts: Modelling Apis with Domain-Driven DesignAPIs with Bounded Contexts: Modelling Apis with Domain-Driven Design
APIs with Bounded Contexts: Modelling Apis with Domain-Driven Design
 
SV.CO’s iterative product development
SV.CO’s iterative product developmentSV.CO’s iterative product development
SV.CO’s iterative product development
 
Experience Session - Hari
Experience Session - HariExperience Session - Hari
Experience Session - Hari
 
The Monitoring and Metic aspects of Eclipse MicroProfile
The Monitoring and Metic aspects of Eclipse MicroProfileThe Monitoring and Metic aspects of Eclipse MicroProfile
The Monitoring and Metic aspects of Eclipse MicroProfile
 
Simplifying Use of Hive with the Hive Query Tool
Simplifying Use of Hive with the Hive Query ToolSimplifying Use of Hive with the Hive Query Tool
Simplifying Use of Hive with the Hive Query Tool
 
2.0 Client Libraries & Using the Java Client by Noah Crowley, Developer Advoc...
2.0 Client Libraries & Using the Java Client by Noah Crowley, Developer Advoc...2.0 Client Libraries & Using the Java Client by Noah Crowley, Developer Advoc...
2.0 Client Libraries & Using the Java Client by Noah Crowley, Developer Advoc...
 
Using the Java Client Library by Noah Crowley, DevRel | InfluxData
Using the Java Client Library by Noah Crowley, DevRel | InfluxDataUsing the Java Client Library by Noah Crowley, DevRel | InfluxData
Using the Java Client Library by Noah Crowley, DevRel | InfluxData
 
APIs : Mapping the way
APIs : Mapping the wayAPIs : Mapping the way
APIs : Mapping the way
 
Lessons learned on the Azure API Stewardship Journey.pptx
Lessons learned on the Azure API Stewardship Journey.pptxLessons learned on the Azure API Stewardship Journey.pptx
Lessons learned on the Azure API Stewardship Journey.pptx
 
Chris Mathias Presents Advanced API Design Considerations at LA CTO Forum
Chris Mathias Presents Advanced API Design Considerations at LA CTO ForumChris Mathias Presents Advanced API Design Considerations at LA CTO Forum
Chris Mathias Presents Advanced API Design Considerations at LA CTO Forum
 
Construindo aplicações com HTML5, WebSockets, e Java EE 7
Construindo aplicações com HTML5, WebSockets, e Java EE 7Construindo aplicações com HTML5, WebSockets, e Java EE 7
Construindo aplicações com HTML5, WebSockets, e Java EE 7
 
Introduction to JSR 354 (Currency and Money) by Anatole Tresch, Werner Keil
Introduction to JSR 354 (Currency and Money) by Anatole Tresch, Werner KeilIntroduction to JSR 354 (Currency and Money) by Anatole Tresch, Werner Keil
Introduction to JSR 354 (Currency and Money) by Anatole Tresch, Werner Keil
 
Punta Dreamin 17 Generic Apex and Tooling Api
Punta Dreamin 17 Generic Apex and Tooling ApiPunta Dreamin 17 Generic Apex and Tooling Api
Punta Dreamin 17 Generic Apex and Tooling Api
 
API Design Workflows
API Design WorkflowsAPI Design Workflows
API Design Workflows
 
Scaling with swagger
Scaling with swaggerScaling with swagger
Scaling with swagger
 
JavaOne 2014 BOF4241 What's Next for JSF?
JavaOne 2014 BOF4241 What's Next for JSF?JavaOne 2014 BOF4241 What's Next for JSF?
JavaOne 2014 BOF4241 What's Next for JSF?
 
AltoWeb_SPEED_Overview-2001
AltoWeb_SPEED_Overview-2001AltoWeb_SPEED_Overview-2001
AltoWeb_SPEED_Overview-2001
 
Middleware in Golang: InVision's Rye
Middleware in Golang: InVision's RyeMiddleware in Golang: InVision's Rye
Middleware in Golang: InVision's Rye
 

Kürzlich hochgeladen

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 

Kürzlich hochgeladen (20)

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 

Moneyandcurrency2