Project

General

Profile

Download (2.28 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2020 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.common.function;
10

    
11
import java.math.BigDecimal;
12
import java.math.MathContext;
13
import java.math.RoundingMode;
14

    
15
/**
16
 * This is class is for testing how BigDecimal actually works
17
 * and how float can be transformed to BigDecimal.
18
 * Can be deleted.
19
 *
20
 * @author a.mueller
21
 * @since 27.04.2020
22
 */
23
public class TestBigDecimal {
24

    
25
    public static void main(String[] args) {
26
        BigDecimal a5 = new BigDecimal("5");
27
        BigDecimal a7 = new BigDecimal("7.0");
28
        BigDecimal a8 = new BigDecimal("8.0");
29
        BigDecimal t = a5.add(a7).add(a8);
30
        t.precision();
31
        BigDecimal a3 = new BigDecimal("3");
32
        BigDecimal d1 = t.divide(a3, RoundingMode.HALF_EVEN);
33
        BigDecimal d2 = t.divide(new BigDecimal("3.00"), MathContext.DECIMAL32);
34
        d2.precision();
35
        BigDecimal d = t.divide(a3, MathContext.DECIMAL128);
36
        BigDecimal m = d2.multiply(a3, MathContext.DECIMAL32).stripTrailingZeros();
37
        BigDecimal m2 = a5.multiply(a7, MathContext.DECIMAL32).stripTrailingZeros();
38
        System.out.println(m);
39
    }
40

    
41

    
42
    public static void main2(String[] args) {
43
        BigDecimal a = new BigDecimal(new Float(2.600f).toString());
44
        System.out.println(a.toString());
45
        System.out.println(a.toPlainString());
46
        System.out.println(a.toEngineeringString());
47
        String str = a.toString();
48
        String strZero = "000";
49
        String strNine = "999";
50
        if (str.contains(strZero)){
51
            int pos = str.indexOf(strZero);
52
            String str2 = str.substring(0, pos);
53
            BigDecimal b = new BigDecimal(str2);
54
            System.out.println(b.toString());
55
        }else if (str.contains(strNine)){
56
            int pos = str.indexOf(strNine);
57
            String str2 = str.substring(0, pos);
58
            BigDecimal b = new BigDecimal(str2);
59
            b = b.add(new BigDecimal(1).movePointLeft(b.scale()));
60
            Float f = Float.parseFloat(b.toString());
61
            System.out.println(b.toString());
62
            System.out.println(f.toString());
63
        }
64
    }
65
}
(1-1/2)