简体   繁体   中英

How to format amount on keyup with toLocaleString ? using minimumFractionDigits?

I'm trying figure out a way to format in live an amount of an input box with local's 'it-IT' and minimumFractionDigits of > 2.

This is my code https://jsfiddle.net/oLgtwjfr/

$(document).on('keyup', '#inputKeyupExample', function(event) {

    var $this = $( this )
    var elementThis = this
    var input = $this.val()

    $this.val(formatNumber(parseInt(input)))
})
function formatNumber(x) {
        return x.toLocaleString("it-IT",
        { minimumFractionDigits: 2 });
}

I except the formatted output of 25000 to be 25.000,00 on keyup. But the actual output is 2,00.

I'm not sure the input box supports that kind of functionality without a lot of extra programming. Also, it would become messy for the user to edit or change the value with lots of numeric formatting in place in that text box. Instead, consider a formatting display separate to the input like the following:

https://jsfiddle.net/declanmcd/nuy9q5g4/4/

So instead of

$this.val(formatNumber(parseInt(input)));

use

$("#output").text(formatNumber(parseInt(input)));

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM