Giter Site home page Giter Site logo

bigdecimal-utils's Introduction

BigDecimal Utils

Comparing BigDecimal is always hard to read and so error prone. I wrote this library to make comparison of BigDecimal more comfortable and more readable.

Is BigDecimal Comparison happens?

Sure! The only reliable way to work with monetary amount is to use BigDecimal. So if you have Money somewhere in your code, you probably faced comparing two BigDecimals a lot.

What's wrong with BigDecimal comparison

Well, First of all you should know that if you use equal method of BigDecimal to compare two objects they only considered equal if they are equal in value and scale thus 2.0 is not equal to 2.00 when compared by equal method. refer to JavaDocs.

To compare BigDecimal without considering their scale we should use compareTo method. So, probably this is the most common way to compare two BigDecimals. However it is so error prone and lacks readability. to feel what it looks like take a look at this line of code :

if(balance.compareTo(amount) < 0) && amount.compareTo(minAmount) >= 0)) {
  // ....
}else {
  // ...
}

the code above try to check condition "balance < amount && amount >= minAmount". You definitely spotted the problem. But how to solve this?

How I Solve the Problem?

BigDecimalUtils is a simple library to make comparing BigDecimal more readable and less error prone. see the same comparison rewritten with this library

import static ir.cafebabe.math.utils.BigDecimalUtils.*;
..
..
..
..
if( is(balance).lt(amount) && is(amount).gte(minAmount) ) {
  // ....
}else {
  // ...
}

Other methods in this library:

  is(bigdecimal).eq(four); //Equal
  is(bigdecimal).gt(two);    //Greater than
  is(bigdecimal).gte(one);   //Greater than equal
  is(bigdecimal).lt(two);  //Less than
  is(bigdecimal).lte(two); //Less than equal

  is(bigdecimal).notEq(four); //Not Equal
  is(bigdecimal).notGt(two);    //Not Greater than
  is(bigdecimal).notGte(one);   //Not Greater than equal
  is(bigdecimal).notLt(two);  //Not Less than
  is(bigdecimal).notLte(two); //Not Less than equal

  is(bigdecimal).isZero(); 	
  is(bigdecimal).notZero(); 
  is(bigdecimal).isPositive(); // greater than zero
  is(bigdecimal).isNegetive(); // less than zero
  is(bigdecimal).isNonPositive(); //less than or equal zero
  is(bigdecimal).isNonNegetive(); //greater than or equal zero

  is(bigdecimal).isNullOrZero(); //is null or zero
  is(bigdecimal).notNullOrZero(); //not null or zero

You can also compare a BigDecimal to other numerical values. for instance instead of writing

  is(bigdecimal).notEq(BigDecimal.valueOf(4));

you can simply write:

  is(bigdecimal).notEq(4);
  is(bigdecimal).notEq(4L); //or
  is(bigdecimal).notEq(4D); //or

PS

I didn't find any library to handle BigDecimal comparison. If you found one please let me know.

bigdecimal-utils's People

Contributors

mortezaadi avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.