0

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.

Pierre
  • 490
  • 1
  • 7
  • 26

3 Answers3

1

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/

ofca
  • 661
  • 5
  • 10
  • 1
    The first method will fail and throw an error on numbers with no decimal places and numbers with one decimal place [jsFiddle example](https://jsfiddle.net/kpno14f1/) .. you should use **`/^(\d*(\.\d{0,2})?)/`** instead. – Mi-Creativity Aug 25 '16 at 11:18
  • 1
    Also, if the O.P needs to have 2 decimal places always, you should use `toFixed(2)` in all three ways – Mi-Creativity Aug 25 '16 at 11:22
  • Actually using `/^(\d*(\.\d{1,2})?)/` makes more sense because if it has zero decimal places it would not have a decimal point. Another thing that you might want to take in considration is, if a string ( like `foo` ) is passed to the 2nd and 3rd methods it will pass as match! [jsFiddle](https://jsfiddle.net/kpno14f1/1/). – Mi-Creativity Aug 25 '16 at 11:55
0

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.

Stark Buttowski
  • 1,799
  • 2
  • 10
  • 21
0

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));
}
Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
  • 1
    Previously you was rounding number, didn't use `floor` and `toFixed`. I canceled my downvote, it seems now your code is working properly. – ofca Aug 25 '16 at 09:58
  • @Techie_Learner, how about the answer now? – Mi-Creativity Aug 25 '16 at 10:28
  • @Mi-Creativity Looks good. hope this works out let me try with few more numbers too . – Techie_Learner Aug 25 '16 at 10:32
  • Even if this works properly it's just looking horrible really. So complicated and dirty (more dirty then using `regex` or `substr`). – ofca Aug 25 '16 at 10:34
  • @ofca, which one is better between the three ways you used in your answer?? using `.substr(0,'15.67789'.indexOf('.')+3)` or `.match(/^(\d*\.\d{2})/)[0];`, or `.replace(/^(\d+\.\d{2}).*/g, '$1')` ?? – Mi-Creativity Aug 25 '16 at 10:56
  • What do you mean by "better"? I just thinking that `regex`/`substr` are simpler. – ofca Aug 25 '16 at 10:58
  • ok let me rephrase, which way of the three you used in your question is more dirty and complicated than the other two? – Mi-Creativity Aug 25 '16 at 10:59
  • If I needed to use it I would prefere `substr` over regex because of performance (I assume it's faster) and because it's easy to understand. – ofca Aug 25 '16 at 11:30
  • Ok, then since you prefer using `substr`, because you assume it's faster and easier to understand, why did you use two other ways beside it in your answer?? – Mi-Creativity Aug 25 '16 at 11:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121834/discussion-between-ofca-and-mi-creativity). – ofca Aug 25 '16 at 11:40