I have number saying 15.67789
.
I want to display only two numbers after the decimal point (i.e 15.67) with out doing any rounding on the number.
For Eg:
15.67789.toFixed(2) returns 15.68
instead
I want to display only 15.67
.
I have number saying 15.67789
.
I want to display only two numbers after the decimal point (i.e 15.67) with out doing any rounding on the number.
For Eg:
15.67789.toFixed(2) returns 15.68
instead
I want to display only 15.67
.
Using regex:
edit: I modified regex to accept numbers without decimal places and numbers with one decimal place (thanks @Mi-Creativity):
var num = 134.35324;
num.toString().match(/^(\d*(\.\d{0,2})?)/)[0];
Working example https://jsfiddle.net/yevwww8m/2/
Using substr
:
var num = 15.67789;
num = num.toString().substr(0,'15.67789'.indexOf('.')+3);
Working example: https://jsfiddle.net/pmmy9o3r/
Just for fun, using regex replace:
var num = 15.57789;
num = num.toString().replace(/^(\d+\.\d{2}).*/g, '$1');
Working example: https://jsfiddle.net/4eq3jd4e/
angular.module('app', [])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app='app'>
<input type='number' ng-model='tex' />
{{tex|number:2}}
</body>
use the number
filter to specify the number of digits to be displayed after the decimal point.
Updated
Simply multiply the number by 100 to move the point two decimal places, floor it then divide it by 100
var nums = [15.67789, 101.62, 134.35324, 0.00658853, 0.002422, 4.27, 1.1095];
for (var i = 0; i < nums.length; ++i) {
var temp = nums[i] * 100;
temp = (('' + temp).split('.')[1] !== '99999999999994') ? (Math.floor(temp) / 100) : nums[i];
console.log(temp.toFixed(2));
}