Reputation: 1453
How to convert $2.50
in cents using angular / JavaScript. For example output should be 250
.
Upvotes: 1
Views: 5975
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
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
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