1

I have number without decimal places and I want to convert it to two decimal places (while keeping zeros) and keep its number type. I have tried it like this:

$scope.invoice_data.form_data.items_shipping_handling = parseFloat(($scope.invoice_data.form_data.items_shipping_handling).toFixed(2));
console.log(typeof $scope.invoice_data.form_data.items_shipping_handling); 

But it parseFloat doesn't take into account decimal places if they are zeros. So if I have 2 I want to convert it to 2.00 //number. Thank you for your time. I mention that the code is in angular so if it is any angular way of doing it I am open to suggestions.

Details: I cannot simply use toFixed(2) I need the result to have decimal places but to keep its number format, toFixed() converts it to string. I need it to be of number type!

Alphonse
  • 661
  • 1
  • 12
  • 29
  • 1
    A float (returned from `parseFloat`) does not have a "number of decimal places". That is a display thing - and you already know how to do that `toFixed(2)`. I dont see what your question is? – Jamiec Jul 20 '17 at 14:29
  • `toFixed` returns a string, but since you need the trailing 0s I am thinking it does not matter what format this display is in right? – Huangism Jul 20 '17 at 14:31
  • If I use toFixed(2) I convert the number to string I want to keep its format. And I tried this by parsing the string from toFixed() to parseFloat, but something like 2.00 gets returned as 2. I want to get 2.00 and have the number format not string – Alphonse Jul 20 '17 at 14:31
  • @Huangism it matters because the result I use in math calculations.... And I return to the view the result of those calculations as well as the formatted number with two decimal places. – Alphonse Jul 20 '17 at 14:32
  • @Alphonse then when you do the calculation, convert it back to a number or use a different variable. Use the string for display and convert when calculating, or use 2 vars one for displaying and one for calculating – Huangism Jul 20 '17 at 14:32
  • @Alphonse or whenever you need the 0s in the view, just do the conversion in the html(angular) or you can make a filter to do the same – Huangism Jul 20 '17 at 14:36
  • @Huangism the problem is that in the view i use a and by returning a variable as string I get console errors (and I want to keep the input as number to use the step="0.1" property) – Alphonse Jul 20 '17 at 14:39
  • The `value` property of an input - even a `number` input can still be a string. In fact I think it *has* to be. – Jamiec Jul 20 '17 at 14:42
  • @Alphonse does the input need to be type number? If so, you should really reword the entire question and ask for this specific issue, basically detailing what we talked about in the comments. Also adding the input html code and the error you get in console – Huangism Jul 20 '17 at 14:42
  • @Jamiec it cannot I get an error about this in the console.. – Alphonse Jul 20 '17 at 14:43
  • [Are you sure?](https://jsfiddle.net/aLwj6hw0/) – Jamiec Jul 20 '17 at 14:45
  • Also - worrying when you say you use the rounded number in calculations. This is a recipe for disaster usually. – Jamiec Jul 20 '17 at 15:02

2 Answers2

2

Numbers dont have a "number of decimal places" - they're internally just a series of 1's and 0's. To display a number to a set number of decimal places you can use toFixed

var value = 2;
console.log(value.toFixed(2));

If you're trying to round a number to a set of decimal places one way is to multiply by 100, round it and then divide by 100

var value = 2.123456
var rounded = Math.round(value*100)/100;
console.log(rounded);
Jamiec
  • 133,658
  • 13
  • 134
  • 193
-2

variable.toFixed(2)

doc here

  • List item
AlainD
  • 6,187
  • 3
  • 17
  • 31