SlideShare ist ein Scribd-Unternehmen logo
1 von 18
Downloaden Sie, um offline zu lesen
Payment
Nick Vincent
neoworks
•  Surely everyone orders and pays the same way as us?
•  In mainland Europe it is common to pay for goods by bank
transfer
•  Brazil charges 60% import tax
•  In Japan it is common to have goods delivered and to pay for
them later
Worldwide
•  3D Secure
•  An unpopular inconvenience
•  Unusual tax regimes
•  US tax calculation is so complex it requires a 3rd
party service
•  Not charging the right amount
•  This is going to upset most people
Problems with payment
•  Promotions
•  Pricing
•  Tax
•  So how do we make sure that you are paying the right
amount?
Complicating the problem
•  Use a double?
• 	
   	
  Memory	
  efficient	
  
• 	
   	
  High	
  precision	
  
• 	
   	
  Fast	
  CPU	
  backed	
  calcula<ons	
  
• 	
   	
  Secret	
  rounding	
  
• 	
   	
  But...	
  rounding	
  problems,	
  but	
  would	
  we	
  really	
  see	
  those	
   	
   	
  
	
   	
  in	
  real	
  life?	
  
Modelling money
•  JVM primitives often have lots of constants to make it easier
for us and prevent the use of magic numbers, like MIN_VALUE.
double adjustment = -5.0D;double threshold =
10.0D;if(adjustment>=Double.MIN_VALUE &&
adjustment<=threshold) { System.out.println("Small
adjustment");} else { System.out.println("Big
adjustment");}
Output:
Big adjustment
Useful constants
•  Double.MIN_VALUE is not really the minimum value
possible.
•  It is “A constant holding the smallest positive nonzero value
of type double, 2-1074”
Urr, why?
• What does this do?
•  double amount = 100.05;
•  double discount = amount * 0.10;
•  double total = amount - discount;
•  NumberFormat money = NumberFormat.getCurrencyInstance();
•  System.out.println("Subtotal="+ money.format(amount));
•  System.out.println("Discount=" + money.format(discount));
•  System.out.println("Total=" + money.format(total));
Subtotal=£100.05Discount=£10.00Total=£90.04
Trouble with double
•  Subtotal=£100.05
•  Discount=£10.00
•  Total=£90.04
• But what were the real values hidden from us?
•  Subtotal=100.049999999999997157829056959599256515502
•  Discount=10.005000000000000781597009336110204458236694
•  Total=90.045000000000001705302565824240446090698
•  Double has helpfully rounded down for us. The rounding behaviour of
NumberFormat can be controlled by RoundingMode in JDK6+.
Explain that
•  But that was a multiplication, surely that can’t happen to a
simple addition?
•  boolean x = (0.7d+0.1d) == (0.9d-0.1d);
•  System.out.println(x);
false
0.7d+0.1d == 0.7999999999999999
0.9d-0.1d == 0.8
Double trouble
•  What could this really mean, it’s just a penny.
Tiny amounts of money
Woman eaten by a computer
Gorman’s salami slicing Blackmail
Ersatz Kryptonite Bad superman
Weather control satellite
•  It’s obviously not right and very salient
•  It undermines the end customer’s confidence
•  Your client thinks you can’t even do basic arithmetic right -
“get me a calculator, I’ll show them how to add up”
So why is this really bad?
•  So why don’t we just use a long instead, like all the
banks do?
•  System needs to be engineered from the ground up to use
this approach
•  Can’t easily carry fractional units through calculation
•  End users/administrators don’t understan the approach when
looking at logs and managing prices
The long way around
•  Infinite precision
•  Internal string representation
•  Slow, but is this really going to affect your system?
Enter big decimal
Here’s a simple example:
•  BigDecimal amount = new BigDecimal(“10”);
•  BigDecimal discount = new BigDecimal(“2”);
•  amount.subtract(discount);
•  System.out.println(amount);
Output:
10
BigDecimals are immutable, so the new amount is calculated and
immediately discarded.
Simple API...
A matter of scale
•  You may have legacy code using doubles
•  How do we interface with it?
double discount = 0.1D; // Call your legacy system here
System.out.println(new BigDecimal(discount));
Output: 0.1000000000000000055511151231257827021181583404541015625
System.out.println(BigDecimal.valueOf(discount));
Output:
0.1
Bridging the gap
Payments nick

Weitere ähnliche Inhalte

Kürzlich hochgeladen

Uk-NO1 Amil baba in pakistan Amil Baba in Karachi Black Magic Islamabad Kala ...
Uk-NO1 Amil baba in pakistan Amil Baba in Karachi Black Magic Islamabad Kala ...Uk-NO1 Amil baba in pakistan Amil Baba in Karachi Black Magic Islamabad Kala ...
Uk-NO1 Amil baba in pakistan Amil Baba in Karachi Black Magic Islamabad Kala ...Amil baba
 
Uae-NO1 Rohani Amil In Islamabad Amil Baba in Rawalpindi Kala Jadu Amil In Ra...
Uae-NO1 Rohani Amil In Islamabad Amil Baba in Rawalpindi Kala Jadu Amil In Ra...Uae-NO1 Rohani Amil In Islamabad Amil Baba in Rawalpindi Kala Jadu Amil In Ra...
Uae-NO1 Rohani Amil In Islamabad Amil Baba in Rawalpindi Kala Jadu Amil In Ra...Amil baba
 
Thoma Bravo Equity - Presentation Pension Fund
Thoma Bravo Equity - Presentation Pension FundThoma Bravo Equity - Presentation Pension Fund
Thoma Bravo Equity - Presentation Pension FundAshwinJey
 
Gender and caste discrimination in india
Gender and caste discrimination in indiaGender and caste discrimination in india
Gender and caste discrimination in indiavandanasingh01072003
 
10 QuickBooks Tips 2024 - Globus Finanza.pdf
10 QuickBooks Tips 2024 - Globus Finanza.pdf10 QuickBooks Tips 2024 - Globus Finanza.pdf
10 QuickBooks Tips 2024 - Globus Finanza.pdfglobusfinanza
 
Uae-NO1 Pakistani Amil Baba Real Amil baba In Pakistan Najoomi Baba in Pakist...
Uae-NO1 Pakistani Amil Baba Real Amil baba In Pakistan Najoomi Baba in Pakist...Uae-NO1 Pakistani Amil Baba Real Amil baba In Pakistan Najoomi Baba in Pakist...
Uae-NO1 Pakistani Amil Baba Real Amil baba In Pakistan Najoomi Baba in Pakist...Amil baba
 
Crypto Confidence Unlocked: AnyKYCaccount's Shortcut to Binance Verification
Crypto Confidence Unlocked: AnyKYCaccount's Shortcut to Binance VerificationCrypto Confidence Unlocked: AnyKYCaccount's Shortcut to Binance Verification
Crypto Confidence Unlocked: AnyKYCaccount's Shortcut to Binance VerificationAny kyc Account
 
Money Forward Integrated Report “Forward Map” 2024
Money Forward Integrated Report “Forward Map” 2024Money Forward Integrated Report “Forward Map” 2024
Money Forward Integrated Report “Forward Map” 2024Money Forward
 
Hello this ppt is about seminar final project
Hello this ppt is about seminar final projectHello this ppt is about seminar final project
Hello this ppt is about seminar final projectninnasirsi
 
Uae-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
Uae-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Uae-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
Uae-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Building pressure? Rising rents, and what to expect in the future
Building pressure? Rising rents, and what to expect in the futureBuilding pressure? Rising rents, and what to expect in the future
Building pressure? Rising rents, and what to expect in the futureResolutionFoundation
 
Overview of Inkel Unlisted Shares Price.
Overview of Inkel Unlisted Shares Price.Overview of Inkel Unlisted Shares Price.
Overview of Inkel Unlisted Shares Price.Precize Formely Leadoff
 
Market Morning Updates for 16th April 2024
Market Morning Updates for 16th April 2024Market Morning Updates for 16th April 2024
Market Morning Updates for 16th April 2024Devarsh Vakil
 
Global Economic Outlook, 2024 - Scholaride Consulting
Global Economic Outlook, 2024 - Scholaride ConsultingGlobal Economic Outlook, 2024 - Scholaride Consulting
Global Economic Outlook, 2024 - Scholaride Consultingswastiknandyofficial
 
ekthesi-trapeza-tis-ellados-gia-2023.pdf
ekthesi-trapeza-tis-ellados-gia-2023.pdfekthesi-trapeza-tis-ellados-gia-2023.pdf
ekthesi-trapeza-tis-ellados-gia-2023.pdfSteliosTheodorou4
 
What is sip and What are its Benefits in 2024
What is sip and What are its Benefits in 2024What is sip and What are its Benefits in 2024
What is sip and What are its Benefits in 2024prajwalgopocket
 
ΤτΕ: Ανάπτυξη 2,3% και πληθωρισμός 2,8% φέτος
ΤτΕ: Ανάπτυξη 2,3% και πληθωρισμός 2,8% φέτοςΤτΕ: Ανάπτυξη 2,3% και πληθωρισμός 2,8% φέτος
ΤτΕ: Ανάπτυξη 2,3% και πληθωρισμός 2,8% φέτοςNewsroom8
 
Unveiling Poonawalla Fincorp’s Phenomenal Performance Under Abhay Bhutada’s L...
Unveiling Poonawalla Fincorp’s Phenomenal Performance Under Abhay Bhutada’s L...Unveiling Poonawalla Fincorp’s Phenomenal Performance Under Abhay Bhutada’s L...
Unveiling Poonawalla Fincorp’s Phenomenal Performance Under Abhay Bhutada’s L...beulahfernandes8
 
OAT_RI_Ep18 WeighingTheRisks_Mar24_GlobalCredit.pptx
OAT_RI_Ep18 WeighingTheRisks_Mar24_GlobalCredit.pptxOAT_RI_Ep18 WeighingTheRisks_Mar24_GlobalCredit.pptx
OAT_RI_Ep18 WeighingTheRisks_Mar24_GlobalCredit.pptxhiddenlevers
 
Banking: Commercial and Central Banking.pptx
Banking: Commercial and Central Banking.pptxBanking: Commercial and Central Banking.pptx
Banking: Commercial and Central Banking.pptxANTHONYAKINYOSOYE1
 

Kürzlich hochgeladen (20)

Uk-NO1 Amil baba in pakistan Amil Baba in Karachi Black Magic Islamabad Kala ...
Uk-NO1 Amil baba in pakistan Amil Baba in Karachi Black Magic Islamabad Kala ...Uk-NO1 Amil baba in pakistan Amil Baba in Karachi Black Magic Islamabad Kala ...
Uk-NO1 Amil baba in pakistan Amil Baba in Karachi Black Magic Islamabad Kala ...
 
Uae-NO1 Rohani Amil In Islamabad Amil Baba in Rawalpindi Kala Jadu Amil In Ra...
Uae-NO1 Rohani Amil In Islamabad Amil Baba in Rawalpindi Kala Jadu Amil In Ra...Uae-NO1 Rohani Amil In Islamabad Amil Baba in Rawalpindi Kala Jadu Amil In Ra...
Uae-NO1 Rohani Amil In Islamabad Amil Baba in Rawalpindi Kala Jadu Amil In Ra...
 
Thoma Bravo Equity - Presentation Pension Fund
Thoma Bravo Equity - Presentation Pension FundThoma Bravo Equity - Presentation Pension Fund
Thoma Bravo Equity - Presentation Pension Fund
 
Gender and caste discrimination in india
Gender and caste discrimination in indiaGender and caste discrimination in india
Gender and caste discrimination in india
 
10 QuickBooks Tips 2024 - Globus Finanza.pdf
10 QuickBooks Tips 2024 - Globus Finanza.pdf10 QuickBooks Tips 2024 - Globus Finanza.pdf
10 QuickBooks Tips 2024 - Globus Finanza.pdf
 
Uae-NO1 Pakistani Amil Baba Real Amil baba In Pakistan Najoomi Baba in Pakist...
Uae-NO1 Pakistani Amil Baba Real Amil baba In Pakistan Najoomi Baba in Pakist...Uae-NO1 Pakistani Amil Baba Real Amil baba In Pakistan Najoomi Baba in Pakist...
Uae-NO1 Pakistani Amil Baba Real Amil baba In Pakistan Najoomi Baba in Pakist...
 
Crypto Confidence Unlocked: AnyKYCaccount's Shortcut to Binance Verification
Crypto Confidence Unlocked: AnyKYCaccount's Shortcut to Binance VerificationCrypto Confidence Unlocked: AnyKYCaccount's Shortcut to Binance Verification
Crypto Confidence Unlocked: AnyKYCaccount's Shortcut to Binance Verification
 
Money Forward Integrated Report “Forward Map” 2024
Money Forward Integrated Report “Forward Map” 2024Money Forward Integrated Report “Forward Map” 2024
Money Forward Integrated Report “Forward Map” 2024
 
Hello this ppt is about seminar final project
Hello this ppt is about seminar final projectHello this ppt is about seminar final project
Hello this ppt is about seminar final project
 
Uae-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
Uae-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Uae-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
Uae-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Building pressure? Rising rents, and what to expect in the future
Building pressure? Rising rents, and what to expect in the futureBuilding pressure? Rising rents, and what to expect in the future
Building pressure? Rising rents, and what to expect in the future
 
Overview of Inkel Unlisted Shares Price.
Overview of Inkel Unlisted Shares Price.Overview of Inkel Unlisted Shares Price.
Overview of Inkel Unlisted Shares Price.
 
Market Morning Updates for 16th April 2024
Market Morning Updates for 16th April 2024Market Morning Updates for 16th April 2024
Market Morning Updates for 16th April 2024
 
Global Economic Outlook, 2024 - Scholaride Consulting
Global Economic Outlook, 2024 - Scholaride ConsultingGlobal Economic Outlook, 2024 - Scholaride Consulting
Global Economic Outlook, 2024 - Scholaride Consulting
 
ekthesi-trapeza-tis-ellados-gia-2023.pdf
ekthesi-trapeza-tis-ellados-gia-2023.pdfekthesi-trapeza-tis-ellados-gia-2023.pdf
ekthesi-trapeza-tis-ellados-gia-2023.pdf
 
What is sip and What are its Benefits in 2024
What is sip and What are its Benefits in 2024What is sip and What are its Benefits in 2024
What is sip and What are its Benefits in 2024
 
ΤτΕ: Ανάπτυξη 2,3% και πληθωρισμός 2,8% φέτος
ΤτΕ: Ανάπτυξη 2,3% και πληθωρισμός 2,8% φέτοςΤτΕ: Ανάπτυξη 2,3% και πληθωρισμός 2,8% φέτος
ΤτΕ: Ανάπτυξη 2,3% και πληθωρισμός 2,8% φέτος
 
Unveiling Poonawalla Fincorp’s Phenomenal Performance Under Abhay Bhutada’s L...
Unveiling Poonawalla Fincorp’s Phenomenal Performance Under Abhay Bhutada’s L...Unveiling Poonawalla Fincorp’s Phenomenal Performance Under Abhay Bhutada’s L...
Unveiling Poonawalla Fincorp’s Phenomenal Performance Under Abhay Bhutada’s L...
 
OAT_RI_Ep18 WeighingTheRisks_Mar24_GlobalCredit.pptx
OAT_RI_Ep18 WeighingTheRisks_Mar24_GlobalCredit.pptxOAT_RI_Ep18 WeighingTheRisks_Mar24_GlobalCredit.pptx
OAT_RI_Ep18 WeighingTheRisks_Mar24_GlobalCredit.pptx
 
Banking: Commercial and Central Banking.pptx
Banking: Commercial and Central Banking.pptxBanking: Commercial and Central Banking.pptx
Banking: Commercial and Central Banking.pptx
 

Empfohlen

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Payments nick

  • 2. •  Surely everyone orders and pays the same way as us? •  In mainland Europe it is common to pay for goods by bank transfer •  Brazil charges 60% import tax •  In Japan it is common to have goods delivered and to pay for them later Worldwide
  • 3. •  3D Secure •  An unpopular inconvenience •  Unusual tax regimes •  US tax calculation is so complex it requires a 3rd party service •  Not charging the right amount •  This is going to upset most people Problems with payment
  • 4. •  Promotions •  Pricing •  Tax •  So how do we make sure that you are paying the right amount? Complicating the problem
  • 5. •  Use a double? •     Memory  efficient   •     High  precision   •     Fast  CPU  backed  calcula<ons   •     Secret  rounding   •     But...  rounding  problems,  but  would  we  really  see  those          in  real  life?   Modelling money
  • 6. •  JVM primitives often have lots of constants to make it easier for us and prevent the use of magic numbers, like MIN_VALUE. double adjustment = -5.0D;double threshold = 10.0D;if(adjustment>=Double.MIN_VALUE && adjustment<=threshold) { System.out.println("Small adjustment");} else { System.out.println("Big adjustment");} Output: Big adjustment Useful constants
  • 7. •  Double.MIN_VALUE is not really the minimum value possible. •  It is “A constant holding the smallest positive nonzero value of type double, 2-1074” Urr, why?
  • 8. • What does this do? •  double amount = 100.05; •  double discount = amount * 0.10; •  double total = amount - discount; •  NumberFormat money = NumberFormat.getCurrencyInstance(); •  System.out.println("Subtotal="+ money.format(amount)); •  System.out.println("Discount=" + money.format(discount)); •  System.out.println("Total=" + money.format(total)); Subtotal=£100.05Discount=£10.00Total=£90.04 Trouble with double
  • 9. •  Subtotal=£100.05 •  Discount=£10.00 •  Total=£90.04 • But what were the real values hidden from us? •  Subtotal=100.049999999999997157829056959599256515502 •  Discount=10.005000000000000781597009336110204458236694 •  Total=90.045000000000001705302565824240446090698 •  Double has helpfully rounded down for us. The rounding behaviour of NumberFormat can be controlled by RoundingMode in JDK6+. Explain that
  • 10. •  But that was a multiplication, surely that can’t happen to a simple addition? •  boolean x = (0.7d+0.1d) == (0.9d-0.1d); •  System.out.println(x); false 0.7d+0.1d == 0.7999999999999999 0.9d-0.1d == 0.8 Double trouble
  • 11. •  What could this really mean, it’s just a penny. Tiny amounts of money Woman eaten by a computer Gorman’s salami slicing Blackmail Ersatz Kryptonite Bad superman Weather control satellite
  • 12. •  It’s obviously not right and very salient •  It undermines the end customer’s confidence •  Your client thinks you can’t even do basic arithmetic right - “get me a calculator, I’ll show them how to add up” So why is this really bad?
  • 13. •  So why don’t we just use a long instead, like all the banks do? •  System needs to be engineered from the ground up to use this approach •  Can’t easily carry fractional units through calculation •  End users/administrators don’t understan the approach when looking at logs and managing prices The long way around
  • 14. •  Infinite precision •  Internal string representation •  Slow, but is this really going to affect your system? Enter big decimal
  • 15. Here’s a simple example: •  BigDecimal amount = new BigDecimal(“10”); •  BigDecimal discount = new BigDecimal(“2”); •  amount.subtract(discount); •  System.out.println(amount); Output: 10 BigDecimals are immutable, so the new amount is calculated and immediately discarded. Simple API...
  • 16. A matter of scale
  • 17. •  You may have legacy code using doubles •  How do we interface with it? double discount = 0.1D; // Call your legacy system here System.out.println(new BigDecimal(discount)); Output: 0.1000000000000000055511151231257827021181583404541015625 System.out.println(BigDecimal.valueOf(discount)); Output: 0.1 Bridging the gap