最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Input type currency format with comma and decimal place 2 - Stack Overflow

programmeradmin3浏览0评论

I just want to ask how to make a JavaScript of currency format of input type text. Is it possible that the numbers will have ma while you type the number? Also, how to make the number fixed with 2 decimal numbers. If I input a 3 decimal placed numbers the last number will round off so it can be 2 decimal.

I have a textbox which accepts numbers only and I want it to automatically convert the numbers into currency format. 1234.556 will be Php 1,234.56. (Php means Philippine peso)

Here's my input type text:

 <input type="text" name='Etxt11' placeholder="Proposed price"  onblur="this.value = 'Php ' + formatNumber(this.value)" />

I just want to ask how to make a JavaScript of currency format of input type text. Is it possible that the numbers will have ma while you type the number? Also, how to make the number fixed with 2 decimal numbers. If I input a 3 decimal placed numbers the last number will round off so it can be 2 decimal.

I have a textbox which accepts numbers only and I want it to automatically convert the numbers into currency format. 1234.556 will be Php 1,234.56. (Php means Philippine peso)

Here's my input type text:

 <input type="text" name='Etxt11' placeholder="Proposed price"  onblur="this.value = 'Php ' + formatNumber(this.value)" />
Share Improve this question edited Jun 4, 2014 at 7:09 Sid M 4,3544 gold badges32 silver badges51 bronze badges asked Jun 4, 2014 at 6:48 user3705672user3705672 651 gold badge1 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

maybe like this?

var formatNumber = function (val){
    return val.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
}
function formatPera(num) {
    var p = num.toFixed(2).split(".");
    return "Php " + p[0].split("").reverse().reduce(function(acc, num, i, orig) {
        return  num + (i && !(i % 3) ? "," : "") + acc;
    }, "") + "." + p[1];
}

var money = 1234.556;
money = formatPera(money);

console.log(money); // Php 1,234.56

Also, credit goes to: https://stackoverflow./a/5342097/1978142

发布评论

评论列表(0)

  1. 暂无评论