Why 0.toFixed(2) print "0" instead of "0.00"? [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Why does 0.toFixed(2) print 0 instead of 0.00?

The correct answer:
Use a variable (noted by Rajesh in the comments)!
var num = 0
var fixedStr = num.toFixed(2);
This just looks better, is easier to understand and also safer, as it will throw errors to you if any occur.
Some Warning
Please note that some interpreters (just like the chrome console) do throw an error if you do 0.toFixed(2), as it is no valid JS to them. If you use a variable or brackets around the 0, it will be okay for them.
Another way for doing it
Also noted in the comments (by 4castle):
You can also use the following:
0..toFixed(2)
As the first dot will be interpreted as a decimal point, this will be okay for the interpreter and be parsed into "0.00".
But please do not use this, use a variable. It just looks horrible and not everyone understands what this should do (or why it magically works).

If you store 0 in a var, or if you use (0), it give 0.00.
(0).toFixed(2)
"0.00"
var x = 0;
x.toFixed(2)
"0.00"

Related

jQuery How To Use Backticks In Append [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I wanted to interpolate variables in strings in JS so I used ``(backticks) as shown here -
How To Interpolate Variables In String in JS
Then, I Wanted To put IF-Statements in jQuery Append So I got this -
IF Statements In jQuery Append
But When I use Both Together , Backticks Don't Output Text As Usual-
$("main").append(`Hello ${my_var}`+(second_var>1?"hi ":"bye")+`Bye ${my_var})`
This Results Only In "hi" , The Backticks Before And After The Ternary Operator Don't Output Anything.
HELP ??
You can do something like the below.
const my_var = "Name";
const seconde_var = 2;
console.log(`Hello ${my_var} ${seconde_var >1 ? "hi": "bye"} bye ${my_var}`);

Javascript checking if a string lentgh is 0 not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a JS function which checks if a user entered string is zero.
if (str.legth = 0) {
alert('Provide at least 1 character to create a folder.');
return;
}
But this seems to let a user pass a zero entered string.
Is there anything missing?
THanks
You've misspelled "length" and you need "==" instead of "=" to check for equailty rather than assigning a value.
If your variable is called str, then length is used to return a boolean value. You need to use either == or === for an actual comparison instead of an assignment. Additionally, a return isn't necessary.

node JS more than is incorrect [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Hi im pretty sure what im about to post might not be enough info (if so please let me know what more is needed). I am using node js and having a really weird error. Below is the code and output.
if (currentPrice > variableData[i].stopLossHard) {
console.log('if')
console.log(currentPrice)
console.log('is more than')
console.log(variableData[i].stoplossHard)
}
Output:
if
92.7
is more than
93.62700000000001
This is consistently happening. I also made sure that both currentPrice and variableData[i].stopLossHard are numbers and not strings (I made sure in the code and in the output its the color of a number not a string)
Any ideas is highly appreciated.
The attribute you print is different than the one you check in the if statement:
(In the if stopLossHard has a capital L, stop-L-ossHard, what you print doesn't)
Try this:
if (currentPrice > variableData[i].stoplossHard) {
console.log('if')
console.log(currentPrice)
console.log('is more than')
console.log(variableData[i].stoplossHard)
}

Regex Issue JavaScript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to take a string and check whether or not it contains a lowercase letter or number, and then if so push that letter or number to an array.
for(let i = 0; i < datearg.length; i++)
{
log.info(datearg.charAt(i));
if(/[a-z]/.test(datearg.charAt(i))) letter_num++; letters.push(datearg.charAt(i));
if(/[0-9]/.test(datearg.charAt(i))) number_num++; numbers.push(datearg.charAt(i));
}
However, both if statements always evaluate to true and the arrays end up containing every single character in datearg. Anyone know why?
if(/[a-z]/.test(datearg.charAt(i))) letter_num++; letters.push(datearg.charAt(i));
is equivalent to
if(/[a-z]/.test(datearg.charAt(i))) { letter_num++; }
letters.push(datearg.charAt(i));
i.e. push is not conditional. This is the primary reason why many style guides heavily discourage control structures without braces (which only take a single statement).

Parse error missing : after property id [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am using some simple javascript as below, but for some reasn the catParam is failing with error missing : after id. please help.
var catParam = "(id=cat00000)";
var inputParams = {serviceID:"getCategories",apiKey="asdfasfgx6",catCriterior:catParam};
Use
var catParam = "(id=cat00000)";
var inputParams = {serviceID:"getCategories",apiKey : "asdfasfgx6",catCriterior:catParam};
Instead - you are using an = instead of a : in your object literal. You assign properties of objects in literals using :.
Check out more info here.
Future reference
Try JsHint or JsLint to verify your code!
Also, if you have clean and organized code, it can make it easier to spot small errors like this, as well as improve the error messages you get (as your error will likely be on a shorter line). Using tools like JsBeautifier can get this done easily.
This would be your code after going through JS Beautifier:
var catParam = "(id=cat00000)";
var inputParams = {
serviceID: "getCategories",
apiKey: "asdfasfgx6",
catCriterior: catParam
};

Categories