仕事上のメモです。
数値を前方0、後方0を排除するJavaソース
------------------------------------------------------
public static String formatTrailingZero(String str) throws Exception {
// 後方ゼロ排除
int x = str.length() - 1;
while( x > 0 && str.charAt(x) == '0'){
x--;
}
str = str.substring(0, x+1);
// 前方ゼロ排除
x = 0;
while( x <str.length() && str.charAt(x) == '0') {
}
str = str.substring(x, str.length());
// 例15.0の場合は、15.となっているので小数点を排除
if (str.substring(str.length()-1).equals(".")){
str = str.substring(0, str.length()-1);
}
// 0.0の場合、何も残らないので0で出力
if (str.equals("")) str = "0";
return str;
}
数値を前方0、後方0を排除するJavaソース
------------------------------------------------------
public static String formatTrailingZero(String str) throws Exception {
// 後方ゼロ排除
int x = str.length() - 1;
while( x > 0 && str.charAt(x) == '0'){
x--;
}
str = str.substring(0, x+1);
// 前方ゼロ排除
x = 0;
while( x <str.length() && str.charAt(x) == '0') {
}
str = str.substring(x, str.length());
// 例15.0の場合は、15.となっているので小数点を排除
if (str.substring(str.length()-1).equals(".")){
str = str.substring(0, str.length()-1);
}
// 0.0の場合、何も残らないので0で出力
if (str.equals("")) str = "0";
return str;
}