SKL
SKL

Reputation: 1453

How to convert decimal number to cents in angular or JavaScript

How to convert $2.50 in cents using angular / JavaScript. For example output should be 250.

Upvotes: 1

Views: 5975

Answers (3)

rama krishna-rk
rama krishna-rk

Reputation: 41

var formatDollarsToCents = function(value) {
    value = (value + '').replace(/[^\d.-]/g, '');
    if (value && value.includes('.')) {
        value = value.substring(0, value.indexOf('.') + 3);
    }
    return value ? Math.round(parseFloat(value) * 100) : 0;
}

Upvotes: 0

Liam Park
Liam Park

Reputation: 511

What do you think about something like this?

var currencyValue = (2.50*100).toFixed(1);

JavaScript toFixed() method in this case is keeping only one decimal after the multiplication

Upvotes: 3

Adnan Aslam
Adnan Aslam

Reputation: 94

Please follow this link:

https://www.npmjs.com/package/dollars-to-cents

Example:

var dollarsToCents = require('dollars-to-cents')

console.log(dollarsToCents('$1,030.25')) // 103025

Upvotes: 0

Related Questions