0

Here I've used jquery-ui.js so that it shows numbers from 0 to 2000000 and I'd like to show long number formatted like 2,000,000

<div class="slider>
<a id="minamount" style="">0</a>
<a id="maxamount" style="">2000000</a>
</div>

javascript:

$(".slider").slider({
            min: 0,
            max: 2000000,
            values: [0, 2000000],
            range: true,

            step: 10,

            slide: function (event, ui) {
               $("#minamount").html(ui.values[0]);
               $("#maxamount").html(ui.values[1]);
            }
});
sepide725
  • 13
  • 4
  • no one can help me؟؟؟ – sepide725 Sep 18 '19 at 15:24
  • Trying to find a duplicate for `.html(ui.values[0].toLocaleString())` – freedomn-m Sep 18 '19 at 15:30
  • TBH your question is a bit confusing as you ask how to submit "3 digits" and in "MVC" - when you want to format a number with commas in jquery-ui. Nothing to do with submitting/MVC and "commas" are not "digits" (eg 1.000). Assumed it's a language barrier and tweaked the question/title – freedomn-m Sep 18 '19 at 15:31
  • Possible Duplicate: https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript And https://stackoverflow.com/questions/3753483/javascript-thousand-separator-string-format And https://stackoverflow.com/questions/5731193/how-do-i-format-numbers-using-javascript – Twisty Sep 18 '19 at 16:23
  • yes,but i can't find the answer – sepide725 Sep 18 '19 at 17:30
  • I literally gave you the answer above: change `$("#minamount").html(ui.values[0]);` to `$("#minamount").html(ui.values[0].toLocaleString());` – freedomn-m Sep 19 '19 at 12:54
  • that's not enough. – sepide725 Sep 19 '19 at 17:07

2 Answers2

1

To format a string using thousands separator (eg 12,345) you can use toLocaleString().

$("#minamount").html(ui.values[0].toLocaleString());

See snippet below.

for (var i = 1.234; i < 10000000; i *= 10)
{  
  console.log(i.toLocaleString());
}
haldo
  • 14,512
  • 5
  • 46
  • 52
1

You can use Intl.NumberFormat() for doing it like -

const numberFormat = new Intl.NumberFormat();
$(".slider").slider({
            min: 0,
            max: 2000000,
            values: [0, 2000000],
            range: true,

            step: 10,

            slide: function (event, ui) {
               $("#minamount").html(numberFormat.format(ui.values[0]));
               $("#maxamount").html(numberFormat.format(ui.values[1]));
            }
});

You can pass a bunch of values for your locale according to the documentation if you want it formatted according to a specific locale.

Note: On the initial load, the slide function is not called, in which case, you could manually call the slider('values') function of the slider after initialization to manually set the HTML in the beginning.

EDIT:

Find a sample Fiddle to play around

Vandesh
  • 6,368
  • 1
  • 26
  • 38