fixed point arithmetics in java with fast performance

前端 未结 4 1847
盖世英雄少女心
盖世英雄少女心 2020-12-17 01:42

I need to represent some numbers in Java with perfect precision and fixed number of decimal points after decimal point; after that decimal point, I don\'t care. (More concre

4条回答
  •  清歌不尽
    2020-12-17 02:29

    Not sure why you need a library for it.

    For example, say you want to add two longs with the same fixed precision

    long c = a + b;
    

    Say you have a fixed precision number you want to multiple by an integer

    long c = a * i;
    

    Say you want to divide a number by a integer rounding to zero

    long c = a / i;
    

    Say you want to print a fixed precision number with 3 decimal places.

    System.out.println(c / 1e3);
    

    Perhaps you are over thinking the problem and assuming you need a library for everything.

    If you are using long or double you might want a small number helper methods for rounding, but you don't need a library as such.

提交回复
热议问题