diff options
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/java7compat/Util.java')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/java7compat/Util.java | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/java7compat/Util.java b/vespajlib/src/main/java/com/yahoo/java7compat/Util.java new file mode 100644 index 00000000000..8a838308fbb --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/java7compat/Util.java @@ -0,0 +1,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; + } + +} |