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?
我将使用NumberFormat.parse()-获取NumberFormat的实例以匹配您的语言环境,创建一个指示期望格式的字符串,然后使用它解析输入。
Before all and as a recommendation for future questions you might ask, please read:
How to create a Minimal Complete and Verifiable Example (MCVE) .
So you can get better help and faster.
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));
}
}
Hopefuly any of these examples might help you in whatever you're trying to do.
Good Luck
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")));
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);
}
You may find, what you want to have.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.