JavaScript Need to get only two digits after decimal

I have string with below format

const number = "21708.333333333336"

Then I pass it to the function to get the number in this format

"21,708.333333333336"

Function I am using for to do this (I have tried this)

let numberFormat = (x) => {
  return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}

But I need only last 2 digits

"21,708.33"

What should be the regex for this?

Answer:1

Try toLocaleString()

let result = Number("21708.333333333336").toLocaleString(undefined, {
  minimumFractionDigits: 2,
  maximumFractionDigits: 2
})

console.log(result)
Answer:2

Before passing it to function which convert it to string use

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

let number = "21708234234324.333333333336"

number = Number(number).toFixed(2);

numberWithCommas(number);
Answer:3

I need to turn a string formatted like that: string = "John:31,Miranda:28" Onto this; obj = { "John" => 31, "Miranda" => 28 } I did this : const class = new Map(); array = string.split(","); ...

I need to turn a string formatted like that: string = "John:31,Miranda:28" Onto this; obj = { "John" => 31, "Miranda" => 28 } I did this : const class = new Map(); array = string.split(","); ...

  1. turn string into array
  2. turn string into int
  3. turn string into list python
  4. turn string into number
  5. turn string into int python
  6. turn string into datetime python
  7. turn string into json
  8. turn string into dictionary python
  9. turn string into array python
  10. turn string into array java
  11. turn string into bytes python
  12. turn string into json python
  13. turn string into int c++
  14. turn string into float python
  15. turn string into list of characters python
  16. turn string into variable name python
  17. turn string into json object
  18. turn string into char
  19. turn string into array ruby
  20. turn string into integer java

I am fetching data from the server and getting the data successfully as you can see in the screenshot, I also checked my code. my code is working properly when I take data manually, but I don't know ...

I am fetching data from the server and getting the data successfully as you can see in the screenshot, I also checked my code. my code is working properly when I take data manually, but I don't know ...

  1. typeerror cannot read property
  2. typeerror cannot read property 'map' of undefined
  3. typeerror cannot read property 'length' of undefined
  4. typeerror cannot read property 'push' of undefined
  5. typeerror cannot read property 'setstate' of undefined
  6. typeerror cannot read property '0' of undefined
  7. typeerror cannot read property 'then' of undefined
  8. typeerror cannot read property 'location' of undefined
  9. typeerror cannot read property 'apply' of undefined
  10. typeerror cannot read property 'requestcontent' of undefined
  11. typeerror cannot read property 'contexttypes' of undefined
  12. typeerror cannot read property 'state' of undefined
  13. typeerror cannot read property 'createelement' of undefined
  14. typeerror cannot read property 'props' of undefined
  15. typeerror cannot read property 'kind' of undefined
  16. typeerror cannot read property 'get' of undefined
  17. typeerror cannot read property 'prototype' of undefined
  18. typeerror cannot read property 'subscribe' of undefined
  19. typeerror cannot read property 'foreach' of undefined
  20. typeerror cannot read property 'name' of undefined

As suggested here, I should use canvas renderer so that images can be displayed in nodes. First of all I create a graphClass: graphClass.g = { nodes: [], edges: [] } Then, I create each node:...

As suggested here, I should use canvas renderer so that images can be displayed in nodes. First of all I create a graphClass: graphClass.g = { nodes: [], edges: [] } Then, I create each node:...

I am working on an Angular project where I want to catch all the encCharge to add them to give a total of all the encCharge in my cf's with a for function. This is my array : produits = [ { ...

I am working on an Angular project where I want to catch all the encCharge to add them to give a total of all the encCharge in my cf's with a for function. This is my array : produits = [ { ...