summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/java7compat/Util.java
blob: 8a838308fbb646e3cd7b885143670e1c439a7aa5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.java7compat;

/**
 * @author <a href="mailto:balder@yahoo-inc.com">Henning Baldersheim</a>
 * @since 5.2
 */

public class Util {
    private static final int javaVersion = Integer.valueOf(System.getProperty("java.version").substring(2,3));
    public static boolean isJava7Compatible() { return javaVersion >= 7; }
    /**
     * Takes the double value and prints it in a way that is compliant with the way java7 prints them.
     * This is due to java7 finally fixing the trailing zero problem
     * @param d the double value
     * @return string representation of the double value
     */
    public static String toJava7String(double d) {
        String s =  String.valueOf(d);
        if ( ! isJava7Compatible() ) {
            s = nonJava7CompatibleString(s);
        }
        return s;
    }
    
    static String nonJava7CompatibleString(String s) {
        if ((s.length() >= 3) && s.contains(".")) {
            int l = s.length();
            for(; l > 2 && (s.charAt(l-1) == '0') && (s.charAt(l-2) >= '0') && (s.charAt(l-1) <= '9'); l--);
            if (l != s.length()) {
                s = s.substring(0, l);
            }
        }
        return s;
    }

}