Javascript 格式化数字,金额



<script language="javascript"> 
function formatNum(num,n)
{//参数说明:num 要格式化的数字 n 保留小数位
    num = String(num.toFixed(n));
    var re = /(-?\d+)(\d{3})/;
    while(re.test(num)) num = num.replace(re,"$1,$2")
    return num;
}

alert(formatNum(1234005651.789,2));
</script>


//Javascript 格式化金额
//格式化:
function fmoney(s, n)
{
n = n > 0 && n <= 20 ? n : 2;
s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
var l = s.split(".")[0].split("").reverse(),
r = s.split(".")[1];
t = "";
for(i = 0; i < l.length; i ++ )
{
t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
}
return t.split("").reverse().join("") + "." + r;
}


复原:
function rmoney(s)
{
     return parseFloat(s.replace(/[^\d\.-]/g, ""));
}


java格式化金额,数字

import java.text.DecimalFormat; 
import java.text.NumberFormat; 
 
public class TestFormatter { 
 
    public static void main(String[] args) { 
        String str = "0600450625465.5626"; 
        TestFormatter t = new TestFormatter(); 
        System.out.println(t.getFormatter(str)); 
        System.out.println(t.getCurrency(str)); 
        System.out.println(t.getDecimalFormat(str)); 
    } 
 
    private String getFormatter(String str) { 
        NumberFormat n = NumberFormat.getNumberInstance(); 
        double d; 
        String outStr = null; 
        try { 
             d = Double.parseDouble(str); 
             outStr = n.format(d); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return outStr; 
    } 
     
    private String getDecimalFormat(String str){ 
        DecimalFormat   fmt   =   new   DecimalFormat("###,###.##");   //也可用000,000.00代替
        String outStr = null; 
        double d; 
        try { 
            d = Double.parseDouble(str); 
            outStr = fmt.format(d); 
        } catch (Exception e) { 
        } 
        return outStr; 
    } 
 
    private String getCurrency(String str) { 
        NumberFormat n = NumberFormat.getCurrencyInstance(); 
        double d; 
        String outStr = null; 
        try { 
             d = Double.parseDouble(str); 
             outStr = n.format(d); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return outStr; 
    } 
} 


测试结果
600,450,625,465.563
¥600,450,625,466
600,450,625,465.56260

转自http://hi.baidu.com/deng1259070/blog/item/0da7d33eee302bd47d1e718f.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值