1

What is the JS regular expression when the pattern must match the first occurrence of ".x", where x can be any digit and . means the decimal point.?

E.g., salary = "5200.2" (it is first defined as a number but then after a few modifications in the lines that follow, it becomes a string before entering the if statement, which is when I am using regular expressions to look for the occurrence of ".x"

var salary =5200.2;

salary = salary.toFixed(2); //string
salary = parseFloat(salary);
salary = salary.toLocaleString();
if (!salary.includes('.')){
    salary = salary + ".00"
}else if (salary.includes("/[.[0-9]]/") && salary.charAt(salary.length) == ""){
    salary = salary + '0';
}

I need help with the salary.includes("/[.[0-9]]/") && part. Regular expression pattern must be ".0"

What my code sample is trying to accomplish is, if my number is 5200.0 or 5200 it will make it 5,200.00 for both.

Gary C
  • 41
  • 1
  • 3
    If you plan on displaying this as money, use `num.toLocaleString("en-US", { style: "currency", currency: "USD" })`. It formats everything for you. – 4castle May 13 '17 at 00:08
  • See also [Regex comma seperating thousands and keeping two decimals](http://stackoverflow.com/questions/43340193/regex-comma-seperating-thousands-and-keeping-two-decimals/). – guest271314 May 13 '17 at 00:14
  • Don't manipulate numbers using regexp. –  May 13 '17 at 02:03
  • @torazaburo What is reason for not manipulating numbers using `RegExp`? – guest271314 May 13 '17 at 02:11
  • @guest271314 Because regexp deals with strings, and numbers are not strings. The language provides much better number formatting capabilities than anything you could hack together yourself. –  May 13 '17 at 04:03
  • @torazaburo Do you mean to convey that you have not used `\d` in any `RegExp` relating to strings in any of your answers? Either approach should return same approach when implemented appropriately. – guest271314 May 13 '17 at 04:11
  • @guest271314 I have used `\d` when needing to match digits in the course of string manipulation, not to format numbers. –  May 13 '17 at 05:07

3 Answers3

1

Do not use regexp to do number manipulation and formatting.

It's hard to tell exactly what you want to do, but I can infer that you want to format a number in a locale-specific way, to exactly two decimal places. The correct way to do that is:

const formatter = Intl.NumberFormat("de-DE", {
   minimumFractionDigits: 2,
   maximumFractionDigits: 2
});

const formattedNumber = formatter.format(5200.5);

console.log(formattedNumber);
   
  • If this is Answer how is Question not duplicate of [Regex comma seperating thousands and keeping two decimals](http://stackoverflow.com/questions/43340193/regex-comma-seperating-thousands-and-keeping-two-decimals/)? Your Answer also ignores the fact that OP is attempting to pass a `RegExp` to `String.prototype.includes()`, which appears to have spawned the Question itself, as that is an issue at `javascript` at Question. – guest271314 May 13 '17 at 04:13
  • @guest271314 This falls into the category of a question asking how to use a pair of pliers to pound in a nail. The underlying question is how to format a number. The use of regexp is simply the OP's (ill-advised) approach to that question. –  May 13 '17 at 05:08
  • _"This falls into the category of a question asking how to use a pair of pliers to pound in a nail."_ Not hardly. `RegExp` is suitable to meet requirement, so too are `Array`, `String` or `Intl` methods. – guest271314 May 13 '17 at 05:48
  • 1
    @guest271314 We'll have to agree to disagree. To format numbers, you should use, uhhh, number formatting utilities. Meanwhile, have fun writing and maintaining your own number formatters using regexp. –  May 13 '17 at 05:51
0

String.prototype.includes() does not expect regular expression for parameter.

You can use RegExp.prototype.test() or String.prototype.match() with RegExp /\.\d/ to match first occurrence of "." followed by digit character.

guest271314
  • 1
  • 15
  • 104
  • 177
-1

You should use .match instead of .includes (which doesn't accept regex patterns) and tweak your regex to look for a dot followed by a digit:

salary.match(/(\.[0-9]+)/)

For example:

function formatSalary(salary) {
  salary = salary.toFixed(2); //string
  salary = parseFloat(salary);
  salary = salary.toLocaleString();
  if (!salary.includes('.')){
      salary = salary + ".00"
  } else if (salary.match(/(\.[\d]{1})$/)) {
      salary = salary + '0';
  }
  return salary;
}

console.log(formatSalary(5200))
console.log(formatSalary(5200.0))
console.log(formatSalary(5200.2))
console.log(formatSalary(5200.21))
Rob M.
  • 35,491
  • 6
  • 51
  • 50
  • if the input salary is 5200.21 it makes it 5200.210 ..? Also, can you explain what each character in the regular expression /(\.[0-9]+)/ does, or can you link me to a regular expressions article page for javascript? I can't find a detailed link on w3schools. – Gary C May 13 '17 at 00:02