Menu

Showing posts with label How to check decimal value in JavaScript?. Show all posts
Showing posts with label How to check decimal value in JavaScript?. Show all posts

Decimal number check using JavaScript

While working with numbers specially decimal number or price or balance. A developer may face an issue where number is not coming in decimal format, and just appearing in round figure or only with one digit after decimal. like 12.2, 20, and 5.5

In this situation how to handle this on frond-end and show balance or price always in two decimal digits. Below is the code sample. 


if (cardBalanceData) {
	console.log('card balance '+cardBalanceData.balance);
	var gcBalance = cardBalanceData.balance;
	gcBalance = (gcBalance.toString().indexOf('.') >= 0) ? gcBalance : gcBalance+'.00';
	console.log("card balance in 2 decimal value:: "+ gcBalance);
}

Here in above JavaScript code, line #4 checking the gcBalance has decimal (.) into this, in case gcBalance doesn't have decimal value then append .00 into gcBalance otherwise print gcBalance as it is.