JQuery Number Formatting

前端 未结 8 933
鱼传尺愫
鱼传尺愫 2020-11-27 15:43

There are way too many questions and answers about this basic functionality, I cannot see the wood for the trees.

In Java there is only one simple answer (jav

8条回答
  •  隐瞒了意图╮
    2020-11-27 16:22

    Using the jQuery Number Format plugin, you can get a formatted number in one of three ways:

    // Return as a string
    $.number( 1234.5678, 2 ); // Returns '1,234.57'
    
    // Place formatted number directly in an element:
    $('#mynum').number( 1234.5678 ); // #mynum would then contain '1,235'
    
    // Replace existing number values in any element
    $('span.num').number( true, 2 ); // Formats and replaces existing numbers in those elements.
    

    If you don't like the format, or you need to localise, there are other parameters that let you choose how the number gets formatted:

    .number( theNumber, decimalPlaces, decimalSeparator, thousandsSeparator )

    You can also get jQuery Number Format from GitHub.

提交回复
热议问题