-2

$('#currency').inputmask("numeric", {
  radixPoint: ".",
  groupSeparator: ",",
  digits: 2,
  autoGroup: true,
  prefix: '$ ', //Space after $, this will not truncate the first character.
  rightAlign: false,
  oncleared: function() {
    self.Value('');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="form-control" id="currency" name="currency">
<script type='text/javascript' src="https://rawgit.com/RobinHerbots/jquery.inputmask/3.x/dist/jquery.inputmask.bundle.js"></script>

input = 15097040
output = $ 15,097,040

It should be like this: $150,970.40. Can anyone help how to get the format right?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
  • Possible duplicate of [How can I format numbers as currency string in JavaScript?](https://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-currency-string-in-javascript) – Igor Sep 25 '19 at 16:48
  • 1
    Divide the input by 100 first? – ceejayoz Sep 25 '19 at 16:48
  • It looks like there are two things going on here. First of all, that is very atypical to have your fractional part appended to the end of the number without a decimal. You're going to have to correct your data before you implement any of the suggested fixes for formatting, none of the answers about putting commas in the number are going to deal with the fact that your last two digits are supposed to be decimal. – Chris Baker Sep 25 '19 at 17:09
  • 1
    Once you fix your data, it looks like your formatter is working correctly. It just has no idea it's supposed to change "12345" into "$123.45" -- that's not standard currency formatting, to change the decimal. As suggested, you'd need to divide by 100 to accomplish that -- or fix your backend to put the decimal into the data in the first place. Formatters do not change data, $124.45 is NOT equal to 12345. – Chris Baker Sep 25 '19 at 17:10
  • 1
    It works fine if you enter `150970.40`. Do you want it to automatically use the last two digits as fractional without having to type the fraction point (like and ATM)? – Racil Hilan Sep 25 '19 at 17:13

1 Answers1

0

try to use: Number.prototype.toFixed

here is a similar case: How can I format numbers as currency string in JavaScript?

Jay Ehsaniara
  • 1,421
  • 17
  • 24