SlideShare une entreprise Scribd logo
1  sur  13
Calculations in Java with open source API
Author: Davor Sauer
www.jdice.org
Content
 Calculations in Java- basics
 Calculations in Java with JCalc
 Comparison of complex calculation with plain Java and JCalc
 Features
 Questions
Calculations in Java- basics
Example: Simple calculation: If you pay $2.00 for a beer that costs $1.10,
how much changes do you get?
System.out.println(2.00 - 1.10); Answers:
a) .9
b) .90
c) 0.899999999999
d) None of the above
BigDecimal payment = new BigDecimal(2.00);
BigDecimal cost = new BigDecimal(1.10);
System.out.println(payment.subtract(cost));
Answers:
a) .9
b) .90
c) 0.899999999999
d) None of the above
=> 0.89999999991118215802998747...
BigDecimal payment = new BigDecimal("2.00");
BigDecimal cost = new BigDecimal("1.10");
System.out.println(payment.subtract(cost));
Answers:
a) .9
b) 0.90
c) 0.899999999999
d) None of the above
Java + JCalc
...more sugar?
Calculations in Java- basics
Example: Simple calculation: If you pay $2.00 for a beer that costs $1.10,
how much changes do you get?
System.out.println(Calculator.builder().val(2.00).sub(1.10).calculate());
Answers:
a) 0.9
b) .90
c) 0.899999999999
d) None of the aboveNum p = new Num(2.00);
Num c = new Num(1.10);
System.out.println(Calculator.builder().val(p).sub(c).calculate()); Answers:
a) .9
b) .90
c) 0.899999999999
d) None of the above
Num p = new Num("2.00");
Num c = new Num("1.10");
System.out.println(Calculator.builder().val(p).sub(c).setStripTrailingZeros(false).calculate());
System.out.println(Calculator.builder("2.00 - 1.10").setStripTrailingZeros(false).calculate());
Answers:
a) .9
b) 0.90
c) 0.899999999999
d) None of the above
0.9
0.899999999999
0.89999999991118215802998747...
Complex example
 Calculate fixed monthly payment for a fixed rate mortgage by Java
BigDecimal interestRate = new BigDecimal("6.5"); // fixed yearly interest rate in %
BigDecimal P = new BigDecimal(200000);
BigDecimal paymentYears = new BigDecimal(30);
// monthly interest rate => 6.5 / 12 / 100 = 0.0054166667
BigDecimal r = interestRate.divide(new BigDecimal(12), 10, BigDecimal.ROUND_HALF_UP).divide(new BigDecimal(100), 10, BigDecimal.ROUND_HALF_UP);
// numerator
// => 0.005416666 * 200000 = 1083.3333400000
BigDecimal numerator = r.multiply(P);
// denominator
r = r.add(new BigDecimal(1)); // => 1.0054166667
BigDecimal pow = new BigDecimal(30 * 12); // N = 30 * 12
// => 1.0054166667 ^ (-30 * 12) ===> 1 / 1.005416666 ^ (30 * 12)
BigDecimal one = BigDecimal.ONE;
BigDecimal r_pow = r.pow(pow.intValue()); // => 1.0054166667 ^ 360 = 6.99179805731691416804....
r_pow = one.divide(r_pow, 10, BigDecimal.ROUND_HALF_UP); // => 1 / 6.991798.. = 0.1430247258
// => 1 - 0.1430247258 = 0.8569752742
BigDecimal denominator = new BigDecimal(1);
denominator = denominator.subtract(r_pow);
// => 1083.3333400000 / 0.8569752742 = 1264.1360522464
BigDecimal c = numerator.divide(denominator, 10, BigDecimal.ROUND_HALF_UP);
c = c.setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println("c = " + c);
 Line of code: 15
Complex example
 Calculate fixed monthly payment for a fixed rate mortgage by Java
Num interestRate = new Num(6.5); // fixed yearly interest rate in %
Num P = new Num(200000);
Num paymentYears = new Num(30);
// monthly interest rate : r = 6.5 / 100 / 12
Num r = Calculator.builder().openBracket().val(interestRate).div(100).closeBracket().div(12).calculate();
// N = 30 * 12 * -1
Num N = Calculator.builder().val(paymentYears).mul(12).mul(-1).calculate();
// c = (r * P) / (1 / (1 + r)^N
Calculator c = new Calculator()
.openBracket()
.val(r).mul(P)
.closeBracket() // numerator
.div() // ---------------
.openBracket() // denumerator
.val(1).sub().openBracket().val(1).add(r).closeBracket().pow(N)
.closeBracket();
Num result = c.calculate().setScale(2);
System.out.println("c = " + result);  Line of code: 8
Complex example
 Calculate fixed monthly payment for a fixed rate mortgage by Java
Num interestRate = new Num("A", 6.5);
Num P = new Num("B", 200000);
Num paymentYears = new Num("C", -30);
Calculator c = Calculator.builder("((A / 100 / 12) * B) / (1 - ((1 + (A / 100 / 12)) ^ (C * 12)))", interestRate, P, paymentYears);
Num result = c.calculate();
System.out.println("c = " + result.setScale(2));
 Line of code: 6
Feature: Show calculation steps
Num interestRate = new Num("A", 6.5);
Num P = new Num("B", 200000);
Num paymentYears = new Num("C", -30);
Calculator c = Calculator.builder("((A / 100 / 12) * B) / (1 - ((1 + (A / 100 / 12)) ^ (C * 12)))", interestRate, P, paymentYears);
c.setScale(10);
c.setTracingSteps(true); // track calculation steps
Num result = c.calculate();
for(Step step : c.getTracedSteps())
System.out.println(step);
System.out.println("c = " + result.setScale(2));
Output:
6.5 / 100 = 0.065
0.065/ 12 = 0.0054166667
0.0054166667 * 200000 = 1083.33334
6.5 / 100 = 0.065
0.065/ 12 = 0.0054166667
1 + 0.0054166667 = 1.0054166667
-30 * 12 = -360
1.0054166667 ^ -360 = 0.1430247258
1 - 0.1430247258 = 0.8569752742
1083.33334/ 0.8569752742 = 1264.1360522464
c = 1264.14
Feature: Modularity
Calculator calc = new Calculator();
calc.use(QuestionOperator.class); // use custom operator '?'
calc.use(SumFunction.class); // use custom function 'sum'
calc.expression("2 ? 2 + 5 - 1 + sum(1,2,3,4)");
@SingletonExtension
public class QuestionOperator implements Operator {
....
// implementation for ‘?’ operator
....
}
@SingletonExtension
public class SumFunction implements Function {
....
// implementation for ‘sum’ function
....
}
Feature: Default configuration
roundingMode=HALF_UP
scale=2
stripTrailingZeros=true
decimalSeparator.out='.'
decimalSeparator.in='.'
numconverter[0]=org.jdice.calc.test.NumTest$CustomObject > org.jdice.calc.test.NumTest$CustomObjectNumConverter
operator[0]=org.jdice.calc.test.CustomOperatorFunctionTest$QuestionOperator
function[0]=org.jdice.calc.test.CustomOperatorFunctionTest$SumFunction
Configure default properties with 'jcalc.properties' file in class path
Questions, ideas, suggestions…
Project page: www.jdice.org

Contenu connexe

Tendances

Tendances (20)

week-2x
week-2xweek-2x
week-2x
 
APLICACIÓN DE LAS DERIVADAS EN CONTABILIDAD Y AUDITORÍA
APLICACIÓN DE LAS DERIVADAS EN CONTABILIDAD Y AUDITORÍAAPLICACIÓN DE LAS DERIVADAS EN CONTABILIDAD Y AUDITORÍA
APLICACIÓN DE LAS DERIVADAS EN CONTABILIDAD Y AUDITORÍA
 
Measuring the Precision of Multi-perspective Process Models
Measuring the Precision of Multi-perspective Process ModelsMeasuring the Precision of Multi-perspective Process Models
Measuring the Precision of Multi-perspective Process Models
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
Muzzammilrashid
MuzzammilrashidMuzzammilrashid
Muzzammilrashid
 
Ques 8
Ques 8Ques 8
Ques 8
 
Forecasting airline passengers with designer machine learning
Forecasting airline passengers with designer machine learningForecasting airline passengers with designer machine learning
Forecasting airline passengers with designer machine learning
 
Experement no 6
Experement no 6Experement no 6
Experement no 6
 
Progr2
Progr2Progr2
Progr2
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Aplicaciones de la derivadas en contabilidad
Aplicaciones de la derivadas en contabilidadAplicaciones de la derivadas en contabilidad
Aplicaciones de la derivadas en contabilidad
 
Terminado matematica ii
Terminado matematica iiTerminado matematica ii
Terminado matematica ii
 
Passenger forecasting at KLM
Passenger forecasting at KLMPassenger forecasting at KLM
Passenger forecasting at KLM
 
Intro To Agda
Intro To AgdaIntro To Agda
Intro To Agda
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS  - SARASWATHI RAMALINGAMPROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS  - SARASWATHI RAMALINGAM
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM
 
7
77
7
 
Problemas de funciones
Problemas de funcionesProblemas de funciones
Problemas de funciones
 
Equation plane
Equation planeEquation plane
Equation plane
 

Similaire à JCalc:Calculations in java with open source API

INSTRUCTIONS Please organize your answers as a Word document with.docx
INSTRUCTIONS Please organize your answers as a Word document with.docxINSTRUCTIONS Please organize your answers as a Word document with.docx
INSTRUCTIONS Please organize your answers as a Word document with.docx
dirkrplav
 
Pre-Calculus Midterm Exam 1 Score ______ ____.docx
Pre-Calculus Midterm Exam  1  Score ______  ____.docxPre-Calculus Midterm Exam  1  Score ______  ____.docx
Pre-Calculus Midterm Exam 1 Score ______ ____.docx
ChantellPantoja184
 
Beautiful Development ブレイクスルー体験記
Beautiful Development ブレイクスルー体験記Beautiful Development ブレイクスルー体験記
Beautiful Development ブレイクスルー体験記
kentaro watanabe
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 
PA 1c. Decision VariablesabcdCalculated values0.21110.531110.09760.docx
PA 1c. Decision VariablesabcdCalculated values0.21110.531110.09760.docxPA 1c. Decision VariablesabcdCalculated values0.21110.531110.09760.docx
PA 1c. Decision VariablesabcdCalculated values0.21110.531110.09760.docx
gerardkortney
 

Similaire à JCalc:Calculations in java with open source API (20)

INSTRUCTIONS Please organize your answers as a Word document with.docx
INSTRUCTIONS Please organize your answers as a Word document with.docxINSTRUCTIONS Please organize your answers as a Word document with.docx
INSTRUCTIONS Please organize your answers as a Word document with.docx
 
Solutions Manual for Basics of Engineering Economy 2nd Edition by Blank
Solutions Manual for Basics of Engineering Economy 2nd Edition by BlankSolutions Manual for Basics of Engineering Economy 2nd Edition by Blank
Solutions Manual for Basics of Engineering Economy 2nd Edition by Blank
 
The Ring programming language version 1.7 book - Part 10 of 196
The Ring programming language version 1.7 book - Part 10 of 196The Ring programming language version 1.7 book - Part 10 of 196
The Ring programming language version 1.7 book - Part 10 of 196
 
Digital electronics k map comparators and their function
Digital electronics k map comparators and their functionDigital electronics k map comparators and their function
Digital electronics k map comparators and their function
 
Pre-Calculus Midterm Exam 1 Score ______ ____.docx
Pre-Calculus Midterm Exam  1  Score ______  ____.docxPre-Calculus Midterm Exam  1  Score ______  ____.docx
Pre-Calculus Midterm Exam 1 Score ______ ____.docx
 
6
66
6
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
Acceptable pins
Acceptable pins Acceptable pins
Acceptable pins
 
Chapter 10
Chapter 10Chapter 10
Chapter 10
 
Beautiful Development ブレイクスルー体験記
Beautiful Development ブレイクスルー体験記Beautiful Development ブレイクスルー体験記
Beautiful Development ブレイクスルー体験記
 
The Ring programming language version 1.6 book - Part 9 of 189
The Ring programming language version 1.6 book - Part 9 of 189The Ring programming language version 1.6 book - Part 9 of 189
The Ring programming language version 1.6 book - Part 9 of 189
 
Text solu2
Text solu2Text solu2
Text solu2
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
"Making tomorrow's code look like today's", Adam Ralph
"Making tomorrow's code look like today's", Adam Ralph"Making tomorrow's code look like today's", Adam Ralph
"Making tomorrow's code look like today's", Adam Ralph
 
PA 1c. Decision VariablesabcdCalculated values0.21110.531110.09760.docx
PA 1c. Decision VariablesabcdCalculated values0.21110.531110.09760.docxPA 1c. Decision VariablesabcdCalculated values0.21110.531110.09760.docx
PA 1c. Decision VariablesabcdCalculated values0.21110.531110.09760.docx
 
Ch2solutions final
Ch2solutions finalCh2solutions final
Ch2solutions final
 
233665105 eng-g-economy
233665105 eng-g-economy233665105 eng-g-economy
233665105 eng-g-economy
 
C Programming Language Part 4
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4
 

Dernier

%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 

Dernier (20)

tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 

JCalc:Calculations in java with open source API

  • 1. Calculations in Java with open source API Author: Davor Sauer www.jdice.org
  • 2. Content  Calculations in Java- basics  Calculations in Java with JCalc  Comparison of complex calculation with plain Java and JCalc  Features  Questions
  • 3.
  • 4. Calculations in Java- basics Example: Simple calculation: If you pay $2.00 for a beer that costs $1.10, how much changes do you get? System.out.println(2.00 - 1.10); Answers: a) .9 b) .90 c) 0.899999999999 d) None of the above BigDecimal payment = new BigDecimal(2.00); BigDecimal cost = new BigDecimal(1.10); System.out.println(payment.subtract(cost)); Answers: a) .9 b) .90 c) 0.899999999999 d) None of the above => 0.89999999991118215802998747... BigDecimal payment = new BigDecimal("2.00"); BigDecimal cost = new BigDecimal("1.10"); System.out.println(payment.subtract(cost)); Answers: a) .9 b) 0.90 c) 0.899999999999 d) None of the above
  • 6. Calculations in Java- basics Example: Simple calculation: If you pay $2.00 for a beer that costs $1.10, how much changes do you get? System.out.println(Calculator.builder().val(2.00).sub(1.10).calculate()); Answers: a) 0.9 b) .90 c) 0.899999999999 d) None of the aboveNum p = new Num(2.00); Num c = new Num(1.10); System.out.println(Calculator.builder().val(p).sub(c).calculate()); Answers: a) .9 b) .90 c) 0.899999999999 d) None of the above Num p = new Num("2.00"); Num c = new Num("1.10"); System.out.println(Calculator.builder().val(p).sub(c).setStripTrailingZeros(false).calculate()); System.out.println(Calculator.builder("2.00 - 1.10").setStripTrailingZeros(false).calculate()); Answers: a) .9 b) 0.90 c) 0.899999999999 d) None of the above 0.9 0.899999999999 0.89999999991118215802998747...
  • 7. Complex example  Calculate fixed monthly payment for a fixed rate mortgage by Java BigDecimal interestRate = new BigDecimal("6.5"); // fixed yearly interest rate in % BigDecimal P = new BigDecimal(200000); BigDecimal paymentYears = new BigDecimal(30); // monthly interest rate => 6.5 / 12 / 100 = 0.0054166667 BigDecimal r = interestRate.divide(new BigDecimal(12), 10, BigDecimal.ROUND_HALF_UP).divide(new BigDecimal(100), 10, BigDecimal.ROUND_HALF_UP); // numerator // => 0.005416666 * 200000 = 1083.3333400000 BigDecimal numerator = r.multiply(P); // denominator r = r.add(new BigDecimal(1)); // => 1.0054166667 BigDecimal pow = new BigDecimal(30 * 12); // N = 30 * 12 // => 1.0054166667 ^ (-30 * 12) ===> 1 / 1.005416666 ^ (30 * 12) BigDecimal one = BigDecimal.ONE; BigDecimal r_pow = r.pow(pow.intValue()); // => 1.0054166667 ^ 360 = 6.99179805731691416804.... r_pow = one.divide(r_pow, 10, BigDecimal.ROUND_HALF_UP); // => 1 / 6.991798.. = 0.1430247258 // => 1 - 0.1430247258 = 0.8569752742 BigDecimal denominator = new BigDecimal(1); denominator = denominator.subtract(r_pow); // => 1083.3333400000 / 0.8569752742 = 1264.1360522464 BigDecimal c = numerator.divide(denominator, 10, BigDecimal.ROUND_HALF_UP); c = c.setScale(2, BigDecimal.ROUND_HALF_UP); System.out.println("c = " + c);  Line of code: 15
  • 8. Complex example  Calculate fixed monthly payment for a fixed rate mortgage by Java Num interestRate = new Num(6.5); // fixed yearly interest rate in % Num P = new Num(200000); Num paymentYears = new Num(30); // monthly interest rate : r = 6.5 / 100 / 12 Num r = Calculator.builder().openBracket().val(interestRate).div(100).closeBracket().div(12).calculate(); // N = 30 * 12 * -1 Num N = Calculator.builder().val(paymentYears).mul(12).mul(-1).calculate(); // c = (r * P) / (1 / (1 + r)^N Calculator c = new Calculator() .openBracket() .val(r).mul(P) .closeBracket() // numerator .div() // --------------- .openBracket() // denumerator .val(1).sub().openBracket().val(1).add(r).closeBracket().pow(N) .closeBracket(); Num result = c.calculate().setScale(2); System.out.println("c = " + result);  Line of code: 8
  • 9. Complex example  Calculate fixed monthly payment for a fixed rate mortgage by Java Num interestRate = new Num("A", 6.5); Num P = new Num("B", 200000); Num paymentYears = new Num("C", -30); Calculator c = Calculator.builder("((A / 100 / 12) * B) / (1 - ((1 + (A / 100 / 12)) ^ (C * 12)))", interestRate, P, paymentYears); Num result = c.calculate(); System.out.println("c = " + result.setScale(2));  Line of code: 6
  • 10. Feature: Show calculation steps Num interestRate = new Num("A", 6.5); Num P = new Num("B", 200000); Num paymentYears = new Num("C", -30); Calculator c = Calculator.builder("((A / 100 / 12) * B) / (1 - ((1 + (A / 100 / 12)) ^ (C * 12)))", interestRate, P, paymentYears); c.setScale(10); c.setTracingSteps(true); // track calculation steps Num result = c.calculate(); for(Step step : c.getTracedSteps()) System.out.println(step); System.out.println("c = " + result.setScale(2)); Output: 6.5 / 100 = 0.065 0.065/ 12 = 0.0054166667 0.0054166667 * 200000 = 1083.33334 6.5 / 100 = 0.065 0.065/ 12 = 0.0054166667 1 + 0.0054166667 = 1.0054166667 -30 * 12 = -360 1.0054166667 ^ -360 = 0.1430247258 1 - 0.1430247258 = 0.8569752742 1083.33334/ 0.8569752742 = 1264.1360522464 c = 1264.14
  • 11. Feature: Modularity Calculator calc = new Calculator(); calc.use(QuestionOperator.class); // use custom operator '?' calc.use(SumFunction.class); // use custom function 'sum' calc.expression("2 ? 2 + 5 - 1 + sum(1,2,3,4)"); @SingletonExtension public class QuestionOperator implements Operator { .... // implementation for ‘?’ operator .... } @SingletonExtension public class SumFunction implements Function { .... // implementation for ‘sum’ function .... }
  • 12. Feature: Default configuration roundingMode=HALF_UP scale=2 stripTrailingZeros=true decimalSeparator.out='.' decimalSeparator.in='.' numconverter[0]=org.jdice.calc.test.NumTest$CustomObject > org.jdice.calc.test.NumTest$CustomObjectNumConverter operator[0]=org.jdice.calc.test.CustomOperatorFunctionTest$QuestionOperator function[0]=org.jdice.calc.test.CustomOperatorFunctionTest$SumFunction Configure default properties with 'jcalc.properties' file in class path