How do I delete the last two numbers after the decimal place?

i have a string like this 21600.00 how do I delete 00 after the dot? I've tried it like this

replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')

but '.00' is not erased, so the assumption that I expect is like this 21,600 or 8,000 etc.

Upvotes: 1

Views: 146

Answers (3)

Sachintha Nayanajith
Sachintha Nayanajith

Reputation: 731

You can try this.

var decimal = 21600.00;
var numString = decimal.toString().split('.')[0];
var integer = parseInt(numString);
console.log(integer);
console.log(Number(numString).toLocaleString('en-US'));

Upvotes: 1

dave
dave

Reputation: 64727

You could try any of these:

const str = '21600.00'
console.log(new Intl.NumberFormat('en-US').format(str));
console.log(Number(str).toLocaleString('en-US'))
console.log(str.slice(0,-3))
console.log(str.split('.')[0])
console.log(str.substring(0, str.indexOf('.')))
console.log(`${Math.round(str)}`)
console.log(`${Math.trunc(str)}`)
console.log(`${parseInt(str)}`)
console.log(Number(str).toFixed())

Upvotes: 5

Always Helping
Always Helping

Reputation: 14570

You can simply use parseInt function to remove .00 This will show the exact number without any zeros added.

If you want to convert back to string format you can use toString() to do that.

If you want to add commas format you can use toLocaleString

Run snippet below.

let string = '21600.00'

//Use parse Int
let int = parseInt(string)

console.log(int.toString()) //Returns string
console.log(int) //Returns number
console.log(int.toLocaleString()) //Returns number with comma added

Upvotes: 1

Related Questions