3

I am using JSP for view. I am using angularjs and javascript

I have a text box, in that text box there is a value called 12.542001. The ng-model should contain the same but when I am showing I want to show 12.54 only on the text box.

If I work with toFixed(2), Then it will round the value, but I don't want to round that value...

sisanared
  • 4,175
  • 2
  • 27
  • 42
user9130953
  • 449
  • 2
  • 13
  • 2
    Possible duplicate of [Truncate number to two decimal places without rounding](https://stackoverflow.com/questions/4187146/truncate-number-to-two-decimal-places-without-rounding) – Overflowrun Mar 07 '18 at 07:12
  • its duplicate.... https://stackoverflow.com/questions/4187146/truncate-number-to-two-decimal-places-without-rounding – Overflowrun Mar 07 '18 at 07:13
  • When you say text box, do you mean an input text control? If so, is it read only? If not, what happens when the user changes the input? – Andy Mar 07 '18 at 14:57

1 Answers1

1

Try this

console.log(parseFloat(Math.round(12.542001 * 100) / 100).toFixed(2));

//or use 

num = "122.542001";
var spV= num.split(".");
console.log(spV[0] +"."+ spV[1].substring(0,2));

Update

If your ng-model name is $scope.totalnumber. then try this

$scope.totalnumber = parseFloat(Math.round($scope.totalnumber * 100) / 100).toFixed(2)
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • this will effect to ng-model, I don't want to round it.Just I want to show the two digits afterdecimal and the ng-model should contain the original value – user9130953 Mar 07 '18 at 07:11
  • perhaps use Math.floor() instead of Math.round() – Blorg Chacha Mar 07 '18 at 07:16
  • I think my explanation is not clear.... ng-model should contain ==>12.542001 but I want to should in the text box 12.54 – user9130953 Mar 07 '18 at 07:16
  • @user9130953 then you should be use separate `ng-model` to one for formatted value and another one for real value. the formatted value is assign to the text box – Ramesh Rajendran Mar 07 '18 at 07:16