0

Using JavaScript, I want to show up to two decimal places, a comma, and a comma after two integers, but I tried to show up to nine digits using integers, but I'm not good at showing two integers and two decimal places.

(like 1.50% or 50.00% or 99.99%...etc)

If enter more digits than that, I want to prevent it from entering.

// Thousand commas (including decimal point)
function numberWithCommas(num) {
  var parts = num.toString().split(".");
  return parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (parts[1] ? "." + parts[1] : "");
}

//Number + - check (remove all non-numeric + - values) Up to 9 numbers
function chkNumber(obj) {
  var tmpValue = $(obj).val().replace(/[^0-9,^\-]/g, '');
  tmpValue = tmpValue.replace(/[,]/g, '');
  tmpValue = tmpValue.substring(0, 9);
  // Forced value change after processing thousands commas
  obj.value = numberWithCommas(tmpValue);

}
// Remove anything other than numeric values
function chkrealNumber(obj) {
  var tmpValue = $(obj).val().replace(/[^0-9,]/g, '');
  obj.value = tmpValue.replace(/[,]/g, '');
}
yhc
  • 23
  • 4
  • I made you a snippet. I had to extract your table tag from the viewport attribute – mplungjan Jul 13 '22 at 19:07
  • Does this answer your question? [Format number to always show 2 decimal places](https://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places) – tacoshy Jul 13 '22 at 19:20
  • Excuse me but I want to know where the snippet is – yhc Jul 14 '22 at 00:10
  • It looks like you accidentally deleted it from the question @yhc in your most recent edit. Hit "edit" then "roll back" to mplungjan's version – Daniel Beck Aug 24 '22 at 13:41

0 Answers0