File: TestBigDecimalMathUtils.java

package info (click to toggle)
libjide-oss-java 3.7.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,444 kB
  • sloc: java: 91,177; xml: 661; makefile: 35
file content (23 lines) | stat: -rw-r--r-- 831 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.jidesoft.utils;

import junit.framework.TestCase;

import java.math.BigDecimal;

public class TestBigDecimalMathUtils extends TestCase {
    public void testPerformRoot() {
        BigDecimal dec = new BigDecimal("25029.33333");
        assertEquals(new BigDecimal("158.20662"), round(BigDecimalMathUtils.sqrt(dec), 5));

        dec = new BigDecimal("36");
        assertEquals(6, BigDecimalMathUtils.sqrt(dec).longValue());

        dec = new BigDecimal("770884");
        assertEquals(878, BigDecimalMathUtils.sqrt(dec).longValue());
    }

    public static BigDecimal round(BigDecimal decimal, int decimalDigits) {
        BigDecimal scale = new BigDecimal(Math.pow(10, decimalDigits));
        return new BigDecimal(Math.round(decimal.multiply(scale).doubleValue())).divide(scale);
    }
}