I have an input which can have a 13 long decimal number with cents and thousands separators. So i can have something like this:
13.213.232.132,13
The same field on my database table is configured to be a NUMBER(13,2) column (oracle)
How can i convert from
13.213.232.132,13 to 13213232132,13
and Vice-Versa?
I'd use NumberFormat.parse() -- get an instance of NumberFormat to match your locale, create a string indicating the format expected, then parse the input with it.
You might want to use DecimalFormat
Here's an example on how to use it.
import java.util.*;
import java.text.*;
class NumberConverter {
public static void main(String args[]) {
double number = 12312312312.31;
String pattern = "###.##";
String pattern2 = "###,###.##";
DecimalFormat myFormatter = new DecimalFormat(pattern);
System.out.println(myFormatter.format(number));
myFormatter = new DecimalFormat(pattern2);
System.out.println(myFormatter.format(number));
}
}
All you need to do with this example is replace , for . and viceversa. (I mean separators).
The actual output for my example is:
12312312312.31
12,312,312,312.31
Here's another option on how to achieve it, code taken from @JonSkeet answer. Setting locale, but again assuming your number is a double.
import java.util.*;
import java.text.*;
class NumberConverter {
public static void main(String args[]) {
double number = 13213232132.13;
NumberFormat f = NumberFormat.getInstance(Locale.GERMANY);
((DecimalFormat) f).applyPattern("##0,000.00");
System.out.println(f.format(number));
}
}
If your input is a String then this could be another way of solving (Since it's a 13 length number that's why I use those limits).
import java.util.*;
import java.text.*;
import java.lang.StringBuilder;
class NumberConverter {
public static void main(String args[]) {
String number = "13.213.232.132,13";
String number1;
String number2;
number1 = number.replace(".", "");
int length;
int length1;
StringBuilder sb = new StringBuilder();
sb.append(number1.charAt(0));
sb.append(number1.charAt(1));
sb.append(".");
int counter = 0;
for(int i = 2; i < 11; i++) {
if(counter == 3) {
sb.append(".");
counter = 0;
}
sb.append(number1.charAt(i));
counter++;
}
sb.append(",");
sb.append(number1.charAt(12));
sb.append(number1.charAt(13));
number2 = sb.toString();
System.out.println(number);
System.out.println(number1);
System.out.println(number2);
}
}
As @kaos said in his comment you can also do it by using DecimalFomatSymbols in this way:
import java.util.*;
import java.text.*;
import java.lang.StringBuilder;
class NumberConverter {
public static void main(String args[]) {
String formatString = "###,###.##";
double number = 12312312312.31;
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.getDefault());
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.');
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);
System.out.println(df.format(number));
}
}
Any of these examples might help you in what you're trying to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With