Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get fractional portion of currency value

The String : $ 22.4969694
Regex Expression : ([.])+

I've tried .test(), but I'm not trying to get a boolean response, I'm trying to get the length number of the . character and slice it.

So if . is detected in the string, it will slice $ 22. off the string and set the variable value to 4969694.

like image 986
Roger Stach Avatar asked Oct 11 '25 19:10

Roger Stach


2 Answers

Without a regex, it's quite straight forward

var str1 = '$ 22.4969694';
var str2 = '$ 22694';

console.log( str1.split('.').pop() )  // 4969694
console.log( str2.split('.').pop() ); // nothing happens
like image 64
adeneo Avatar answered Oct 14 '25 09:10

adeneo


I am not 100% sure that I follow what you want your final result(s) to look like but this is what I have come up with that may work for you.

Based on what values you want back this works.

let vals = ['$ 22.4969694', '$ 22', '$ 22567.00']

vals.forEach(val => {
  let v = val.replace(/^.*\./, '')

  console.log(v)
})
like image 22
Get Off My Lawn Avatar answered Oct 14 '25 09:10

Get Off My Lawn