In Javascript - Why this doesn't fail? [duplicate] - javascript

This question already has answers here:
difference between numbers using or without using variable
(3 answers)
Closed 7 years ago.
100..toString() // "100"
100 . toString() // "100"
Not able to understand why it doesn't fail? What is happening here?

Space doesn't matter but allow you to separate the floating point number and the method so 100 . toString(); returns output correctly without throwing error. And with 100.toString(), it would throw an error as JavaScript store the number as floating point number so you need to separate between floating point dot and the invocation dot so 100..toString(); returns correct result.
See related question for more info.

The first dot ensures the "100" is parsed as a float. The second accesses the member.

Related

Call method directly on number [duplicate]

This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 4 years ago.
I recently implemented a very simple method to extend the base JavaScript Number Class and tried to call it directly on an entered number in the browser console.
123.myMethod();
But it is not working as expected, it only says: "Invalid or unexpected token"
I was unsure, if I can call Number Methods directly on entered numbers, so I tried standard methods like .toFixed():
123.toFixed(1);
But this also isn't working.
Only if I write a float, I can call Number methods:
123.0.toFixed(1);
It also works, if I put the Integer inside brackets:
(123).toFixed(1);
So my question is:
Why are Integers not implicitly casted to Number and why can't I use Number methods on them?
The . tells the JavaScript interpreter to be a decimal point, so it is expecting more numbers. In JavaScript there is only floats, no integers.

Smaller number is greater than bigger number [duplicate]

This question already has answers here:
Why parseFloat in javascript returns string type for me?
(2 answers)
Closed 4 years ago.
In my JS, I'm doing a check to see if one number is greater than the other. I'm attaching the image of Chrome DevTools:
As you can see here, the code has made it inside the if statement. On the right, in the Watch, you can see amount = "3.00" and available = "261.60".
What would cause the smaller number to be greater than the bigger number?
You are are comparing strings. In the code you are calling parseFloat, which is the right idea, but it's followed by toFixed(). toFixed() returns a string.
console.log(typeof parseFloat("3.00").toFixed(2))
You need to make sure you're comparing numbers. An easy way is:
if(+amount > +available)
Alternatively, don't call toFixed() until it's time to display the number.

Javascript : Math.round is not working correctly for specific digits [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 5 years ago.
I am facing weird issue during rounding the value using javascript. Some of the value is not rounding in correct format.
var n =17.955 ;
var roundedPrice;
roundedPrice = Math.round(n*100)/100;
console.log(roundedPrice); // It returns 17.95 instead of 17.96
It is happening for some specific values like 16.955, 17.955, 18.955, 19.955. Except these values like 1.955, 12.955, 20.955, 27.955 ... This round function return correct values.
Edited : It is happening with 17.955 only. This returns correct result with 17.9555 ( 3 times 5).
Thanks in advance.
You can use either Math.ceil() to get the expected result.

Why does a method like `toString` require two dots after a number? [duplicate]

This question already has answers here:
Why does 10..toString() work, but 10.toString() does not? [duplicate]
(3 answers)
Closed 5 years ago.
What is the logic behind 42..toString() with ..?
The double dot works and returns the string "42", whereas 42.toString() with a single dot fails.
Similarly, 42...toString() with three dots also fails.
Can anyone explain this behavior?
console.log(42..toString());
console.log(42.toString());
When you enter 42.toString() it will be parsed as 42 with decimal value "toString()" which is of course illegal. 42..toString() is simply a short version of 42.0.toString() which is fine. To get the first one to work you can simply put paranthesis around it (42).toString().
it is like 42.0.tostring() so it show's decimal point you can use (42).toString() 42 .toString() that also work there is space between 42 and dot. This is all because in javascript almost everything is object so that confusion in dot opt.
With just 42.toString(); it's trying to parse as a number with a decimal, and it fails.
and when we write 42..toString(); taken as 42.0.toString();
we can get correct output by
(42).toString();
(42.).toString();
Can refer Link for .toString() usage

Javascript convert float to Exponential [duplicate]

This question already has answers here:
How can I convert numbers into scientific notation?
(3 answers)
Closed 9 years ago.
I have to convert the float to exponential..
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000021
Using
parseFloat(result)
Gives 2.1e-87
But
10000000
Gives 10000000 (same) but I like to get 100.0e+3 etc..So I used parseFloat(result).toExponential(3); But the problem here is it is truncating everything ev even if it has more values for example 111222333 it makes it as 111.22+3 so while I reconverting as integer I am cannot get the original value..
Is there any javascript function to achieve this or how can I achieve this..
Thanks in advance..
If you use toExponential() with out the parameter, it will not drop the fractions:
console.log( (111222333).toExponential() ); // "1.11222333e+8"
The parameter specifies the amount of digits after the decimal point. If you drop it, it displays as many digits as needed (wrt to the double precision JavaScript uses internally).

Categories

Resources