In Java, can I use DecimalFormat to enter a dash (-) between numbers?
Can I use DecimalFormat
to enter a dash (-) between numbers? I have been trying to do it, but somehow when I run the program the dash ends up at the end of the numbers like: 129329-
I want it to look like this: 129-32开发者_JAVA百科9
Here is how you can do this.
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
public class NewClass
{
public static void main(String[] args)
{
DecimalFormatSymbols phoneNumberSymbols = new DecimalFormatSymbols();
// Use space not comma to thousands: 10 000 not 10,000.
phoneNumberSymbols.setGroupingSeparator('-');
DecimalFormat phoneNumberFormat = new DecimalFormat("####,###,###", phoneNumberSymbols);
System.out.println("Some mobile number:" + phoneNumberFormat.format(567884968L));
}
}
If your phone number is a string, here is the solution :
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
public class NewClass {
public static void main(String[] args) {
long lg_tel = Long.parseLong(example.getString("tel")); // String to Long
DecimalFormatSymbols phoneNumberSymbols = new DecimalFormatSymbols();
// Use space not comma to thousands: 10 000 not 10,000.
phoneNumberSymbols.setGroupingSeparator('-');
DecimalFormat phoneNumberFormat = new DecimalFormat("####,###,###", phoneNumberSymbols);
(...)
tv.setText(phoneNumberFormat.format(lg_tel).toString());
}
}
精彩评论