Reputation: 1171
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?
Upvotes: 3
Views: 372
Reputation: 370
A very ugly approach, maybe it helps:
public static void main(final String[] args) {
final String str = "12.345.678.912,13";
final String improved = str.replaceAll("\\.", "").replaceAll(",", ".");
System.out.println(improved);
final double dbl = Double.parseDouble(improved);
System.out.println("For database: " + dbl);
// You may even pass a Locale to String::format
final String str2 = String.format("%,f", dbl);
System.out.println(str2);
final String stringResult = str2.substring(0, str2.length() - 4);
System.out.println(stringResult);
}
Upvotes: 0
Reputation: 11153
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.
Upvotes: 2
Reputation: 492
Use DecimalFormat:
DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(new Locale("pt", "br"));
formatter.applyPattern("#0.##"); //i guess this is the pattern you need.
System.out.println(formatter.format(new BigDecimal("13213232132.13")));
Upvotes: 1
Reputation: 13133
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.
Upvotes: 2