What is the language? Java? You can use the split() function of String if you just want to keep numbers before "." :
String mystring = "34.000000";
String correctstring[] = mystring.split(".");
System.out.println(correctstring[0]);
// display : 34
it will delete all digits after "." !
Answer from Crow EH on Stack OverflowWhat is the language? Java? You can use the split() function of String if you just want to keep numbers before "." :
String mystring = "34.000000";
String correctstring[] = mystring.split(".");
System.out.println(correctstring[0]);
// display : 34
it will delete all digits after "." !
Inside your method that converts a BigDecimal into a String, you can use BigDecimal.setScale() to set the number of digits after the decimal point. For example:
BigDecimal d = new BigDecimal("34.000000");
BigDecimal d1 = d.setScale(2, BigDecimal.ROUND_HALF_UP); // yields 34.00
BigDecimal d2 = d.setScale(0, BigDecimal.ROUND_HALF_UP); // yields 34
You need to escape the ., as it is a special character in Regex that matches any character. You also have to remove the ^, which anchors at the beginning of the number.
str.replaceAll("\\.0*$", "");
You can use a lookbehind if you want to make sure there is a number in front of the dot, like this:
str.replaceAll("(?<=^\\d+)\\.0*$", "");
The lookbehind (the (?<=...) part) is not a part of the match, so it will not be replaced, but it still has to match for the rest of the regex to match.
Nope. Use this:
str.replaceAll("[.0]+$", "");
It's because cellSONum.getNumericCellValue() is returning a floating point type. If you force it to an integer before calling valueOf(), you should get the string representation in an integral form, if indeed that's what you want for all possibilities:
SONum = String.valueOf((int)cellSONum.getNumericCellValue());
You can see this in the following code:
class Test {
public static void main(String[]args) {
double d = 1234;
System.out.println(String.valueOf(d));
System.out.println(String.valueOf((int)d));
}
}
which outputs:
1234.0
1234
However, if you want to just get rid of .0 at the end of any string but allow non-integral values to survive, you can just remove the trailing text yourself:
class Test {
public static void main(String[]args) {
double d1 = 1234;
double d2 = 1234.567;
System.out.println(String.valueOf(d1).replaceFirst("\\.0+$", ""));
System.out.println(String.valueOf(d2).replaceFirst("\\.0+$", ""));
}
}
That snippet outputs:
1234
1234.567
Try with split().
SONum = String.valueOf(cellSONum.getNumericCellValue());
SONum = SONum.split("\\.")[0];
When you split 40002547.0 with . ,the split function returns two parts and the first one you need.
replaceAll replaces a regex. You should use replace instead:
String s2 = s.replace(".", "");
Instead of replaceAll use replace method of String:
String s2 = s.replace(".", "");
As "." has a special meaning in regex and replaceAll treats source string i.e. first parameter as regex.
The Java library has a built-in class that can do this for it. It's BigDecimal.
Here is an example usage:
BigDecimal number = new BigDecimal("10.2270");
System.out.println(number.stripTrailingZeros().toPlainString());
Output:
10.227
Note: It is important to use the BigDecimal constructor that takes a String. You probably don't want the one that takes a double.
Here's a method that will take a Collection<String> and return another Collection<String> of numbers with trailing zeros removed, gift wrapped.
public static Collection<String> stripZeros(Collection<String> numbers) {
if (numbers == null) {
throw new NullPointerException("numbers is null");
}
ArrayList<String> value = new ArrayList<>();
for (String number : numbers) {
value.add(new BigDecimal(number).stripTrailingZeros().toPlainString());
}
return Collections.unmodifiableList(value);
}
Example usage:
ArrayList<String> input = new ArrayList<String>() {{
add("10.0"); add("10.00"); add("10.10"); add("10.2270");
}};
Collection<String> output = stripZeros(input);
System.out.println(output);
Outputs:
[10, 10, 10.1, 10.227]
Try
DecimalFormat decimalFormat = new DecimalFormat("#.##");
String string1 = decimalFormat.format(10.000);