将String类型的数字格式化成逗点加小数点形式如“1234”变成"1,234.00"

caihorse 2008-03-18 09:35:40
能不能帮我分析下 下面这个题目啊
/** (3)
* 将money格式化成逗点加小数点形式
* 如:formatMoney("123456")将返回"123,456.00"
* formatMoney("1234567.10")将返回"1,234,567.10"
* 当输入为不合法的数字时抛出异常
*/
public String formatMoney(String money) throw Exception;
...全文
594 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
楼主你太强大了,一年多前的帖子啊!
caihorse 2009-04-27
  • 打赏
  • 举报
回复
谢谢各位!!!!
zjhlht 2008-03-19
  • 打赏
  • 举报
回复
要好好看看正则表达式了~~~~
  • 打赏
  • 举报
回复
怎么能不要呢,如果不要的话,数字是 123456.00 就会变成 ,123,456.00 的。
logi22 2008-03-19
  • 打赏
  • 举报
回复
money = money.replaceAll("(?<=\\d)(?=(?:\\d\\d\\d)+\\.)", ",");

火龙果的正则前面那句(?<=\\d)是做什么的
可以不要的吧?
huangyong87 2008-03-19
  • 打赏
  • 举报
回复
使用DecimalFormat或者,你有时间,也可以试试NumberFormat
同意2楼的做法。
  • 打赏
  • 举报
回复
public class Test {

public static void main(String[] args) {
String str = "1234567";
System.out.println(formatMoney(str));
str = "1234567.10";
System.out.println(formatMoney(str));
}

public static String formatMoney(String money) {
if(!money.matches("\\d+(\\.\\d{1,2})?")) {
throw new IllegalArgumentException("money format error!");
}
int pointIndex = money.indexOf(".");
if(pointIndex < 0) {
money = money + ".00";
}
if(pointIndex == money.length() - 2) {
money = money + "0";
}
money = money.replaceAll("(?<=\\d)(?=(?:\\d\\d\\d)+\\.)", ",");
return money;
}
}
dracularking 2008-03-19
  • 打赏
  • 举报
回复

public static void formatMoney(String money) {
if (!money.matches("(?!(\\.|[0]{2,}))[\\d\\.]*(?<!\\.)")){
System.out.println("Invalid Number");
return;
}
int integralNumber = money.indexOf('.');
int newCharLength = (integralNumber - 1) / 3 + money.length();

char[] c = new char[newCharLength];

for (int i = integralNumber % 3; i < integralNumber; i += 3) {
if (i != 0) {
c[i++] = ',';
}
}

int count = 0;
for (int i = 0; i < newCharLength; i++) {
if (c[i] != ',') {
c[i] = money.charAt(count++);
}
}
System.out.println("The answer is: ");
System.out.println(c);
}
bootupnow 2008-03-18
  • 打赏
  • 举报
回复
学习,好像在哪儿看到过例子
iihero 2008-03-18
  • 打赏
  • 举报
回复
使用DecimalFormat或者,你有时间,也可以试试NumberFormat
同意2楼的做法。
fosjos 2008-03-18
  • 打赏
  • 举报
回复
DecimalFormat
eimhee 2008-03-18
  • 打赏
  • 举报
回复
package test;

import java.text.DecimalFormat;

public class Testtt {

public String formatMoney(String money) throws Exception {
Double num;
try {
num =Double.parseDouble(money);
} catch (Exception e) {
throw new IllegalArgumentException("please input a number");
}

DecimalFormat formater = new DecimalFormat("###,###.00");

return formater.format(num);
}

public static void main(String[] args) {
Testtt tt = new Testtt();
try {
System.out.println(tt.formatMoney("123456"));
System.out.println(tt.formatMoney("1234567.10"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
eimhee 2008-03-18
  • 打赏
  • 举报
回复
public String formatMoney(String money) throw Exception
{
try{
Interger.pase(money);
}catch(Exception e)
{
throw IlleageArguException();
}

DicimalFormater formater = new Dicimalformater("###,###,###.00");

return formater.format(money);
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧