Javascript - Converting a number to two decimal places while keeping zeros - javascript

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!

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);

variable.toFixed(2)
doc here
List item

Related

Javascript How to convert a decimal number to a string with specific number of decimal places

In a javascript code, I have a requirement to format a decimal number to a specific number of decimal places and get its exact string representation. For example, If the number is 999999999.9 and the number of decimal places is 8, then the expected value should be "999999999.90000000"
When the Number.toFixed(8) is used it returns a rounded value which is not what I want. Please refer the below code
var num = 999999999.9
var string_rep = num.toFixed(8)
>> the value of string_rep is "999999999.89999998"
I used num.toString() and tried to manually format the decimal part by adding/removing digits, but it does not work for very small numbers like "0.00000008" as the function toString() returns
the scientific notation, i.e. something like "9e-8"
So what should be the proper approach for this?
Number.prototype.toLocaleString will do the trick
num.toLocaleString('en-US', {minimumFractionDigits: 8, useGrouping: false})//"999999999.90000000"

How JavaScript writes a number with 2 decimal place and it will show 0 in decimal?

JavaScript writes a number with 2 decimal place and it will show 0 in decimal?
I have no worry about round off the number because I am accepting only 2 decimal place number
let num = 2.60
console.log(num) // result 2.6
It will return 2.6 but I want 2.60. I can use to.Fixed(2) but it will return in string format.
let num = 1.23
console.log(num) // result 1.23
I know to.Fixed() but it returns string. I want number format and it show 1.00 and 1.60 in number format not in the string format. I have used parseFloat(num.toFixed(2)) but it does not show last 0 value in decimal.
You can never change how the javascript engine represent float numbers, if it is a number and has enough leading zeros that the engine considers that it should cut it then it would do.
1.60 in floating point is actually more like 1.6000000000000000888, you can see that yourself by trying
console.log(1.60 === 1.6000000000000000888) // true
The solution is to use toFixed whenever you want to display the value in the format you need, and when you need to do arithmetic operations convert that value back using parseFloat - just like you did, but just have separate variables for views and arithmetic.

How can I parse a string as an integer and keep decimal places if they are zeros?

I have these strings: "59.50" & "30.00"
What I need to do is convert them to integers but keep the trailing zeros at the end to effectively return:
59.50
30.00
I've tried:
Math.round(59.50 * 1000) / 1000
Math.round(30.00 * 1000) / 1000
but ended up with
59.5
30
I'm assuming I need to use a different method than Math.round as this automatically chops off trailing zeros.
I need to keep these as integers as they need to be multiplied with other integers and keep two decimals points. T thought this would be fairly straight forward but after a lot of searching I can't seem to find a solution to exactly what I need.
Thanks!
Your premise is flawed. If you parse a number, you are converting it to its numerical representation, which by definition doesn't have trailing zeros.
A further flaw is that you seem to think you can multiply two numbers together and keep the same number of decimal places as the original numbers. That barely makes sense.
It sounds like this might be an XY Problem, and what you really want to do is just have two decimal places in your result.
If so, you can use .toFixed() for this:
var num = parseFloat("59.50");
var num2 = parseFloat("12.33");
var num3 = num * num2
console.log(num3.toFixed(2)); // 733.64
Whenever you want to display the value of the variable, use Number.prototype.toFixed(). This function takes one argument: the number of decimal places to keep. It returns a string, so do it right before viewing the value to the user.
console.log((123.4567).toFixed(2)); // logs "123.46" (rounded)
To keep the decimals - multiply the string by 1
example : "33.01" * 1 // equals to 33.01
Seems you are trying to retain the same floating point, so better solution will be some thing like
parseFloat(string).toFixed(string.split('.')[1].length);
If you want numbers with decimal points, you are not talking about integers (which are whole numbers) but floating point numbers.
In Javascript all numbers are represented as floating point numbers.
You don't need the trailing zeros to do calculations. As long as you've got all the significant digits, you're fine.
If you want to output your result with a given number of decimal values, you can use the toFixed method to transform your number into a formatted string:
var num = 1.5
var output = num.toFixed(2) // '1.50'
// the number is rounded
num = 1.234
output = num.toFixed(2) // '1.23'
num = 1.567
output = num.toFixed(2) // '1.57'
Here's a more detailed description of toFixed: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

Round float to 2 decimals javascript

I have the problem that when i round a number to 2 decimals the parseFloat() function removes .00 from the number. I have tried
var num = parseFloat(Math.round(19 * 100) / 100).toFixed(2);
The return: num="19.00"
The return i need: num = 19.00
I know 19 = 19.00, but i am using a service that always require two decimals .00
The function returns a string with the right value. When i parse it to float the .00 is removed.
You cannot get 19.00 as float, only as string, because numbers always remove trailing zeros.
Maybe you can show us a bit more code to get an idea, there you need these trailing zeros?
Numbers do and can not hold information about their representation. They are only a numerical value.
When you display a number using window.alert, console.log or similar, you are not looking at a number, but at a string. Those display functions convert numbers to strings before displaying them. Number.toFixed also converts numbers into strings, with the difference being that it rounds them to two decimal places, so you end up with another representation of the same number.
What I am trying to say is that to display a number, you cannot get around converting it to a string. Whether you do it explicitly or the display function does it for you. When you send the number to the service that you are using, you are probably also sending a string (JSON, XML, etc. are always strings once you send them). If you need the value of the number for calculations, use it, then convert it in the end. No matter how, you have to do it in the end if you want those 0's at the end.

Convert a Decimal number to float and truncate it

I have a number
For example:
8183
What I need is to convert it to a float number-
For example 8183
(8183).toFixed(2);
will return me
8183.00
But I need to truncate it further, so the final number will be
8.18
So basically I need to make it float number with just 2 decimal places.
I tried using the Math.floor and ceil but couldnt figure it out!
Well what you're trying to accomplish is not completely clear, but I think that if you start by dividing by 1000, then call toFixed on it, it will give you the desired result.
var before = 8183;
var after = (before / 1000).toFixed(2); //8.18
You could divide by 10 until you are less than 10:
var digits = 8183;
while((digits = digits/10) > 10) {}
digits = digits.toFixed(2); // 8.18
For negative numbers, you could want to store a boolean value and use Math.abs(digits).
For numbers less than 0, you would want to multiple instead of divide.
If all you really want is scientific notation use toExponential(2)

Categories

Resources