I'm building a calculator to test out my Vue JS skills. I have the fundamentals working or at least set-up, however if I enter more than one number (e.g. 34) I can't get it to read all numbers (e.g. 34) as a whole value; rather it adds 3 and 4 to the total. The snippet I'm having trouble with is here:
press: function(num) {
if(this.currentNum == 0)
{
this.currentNum = num;
this.output = num.toString();
}
else {
this.output += num.toString();
this.currentNum = this.output.parseInt();
}
},
Here's the whole thing on CodePen for better context - https://codepen.io/ajm90UK/pen/BZgZvV
I think this is more of a JS issue rather than anything specific to Vue but even with parseInt() on the string I'm having no luck. Any suggestions on how I can read the display (output) would be greatly appreciated, I've been at this for hours now!
parseInt() is not a method of the String prototype but rather a built-in function.
Update this line:
this.currentNum = this.output.parseInt();
to this:
this.currentNum = parseInt(this.output);
And it is a good idea to specify the radix in the second parameter - perhaps 10 in most all cases, for decimal numbers:
this.currentNum = parseInt(this.output, 10);
That way, if a value like 022 is entered, it isn't interpreted as the octal value (I.e. Decimal number 18).
Related
On this page, https://emperorbob7.github.io/JSheets/, I have a function called TYPE, syntax for it is linked on the page, the RegEx used for the decimal detection function is located in codeblock below*
Once I put in too many numbers however, the TYPE says the cell contains a decimal despite none being there. Is this an automatic function that adds a . whenever a number exceeds a certain limit?
Example case: 3123123123123123123122312312312
Output: Decimal
Edit:
function TYPE() {
const regex = /\.[0-9]/;
if(arguments[0] == "true" || arguments[0] == "false")
return "Boolean";
if(isNaN(arguments[0]))
return "String";
else if(regex.test(arguments[0]))
return "Decimal";
else
return "Integer";
}
Code^ Sorry for not posting it before, will keep it in mind for the future.
Sorry for the badly worded question, thanks in advance
You have an integer that is larger than the Number object can handle (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER), so when it's converted to a string for the regex it becomes an exponential value.
Try console.log(3123123123123123123122312312312); and you will get 3.123123123123123e+30
Or
let val = 3123123123123123123122312312312;
val.toString();
"3.123123123123123e+30"
You can also test your value with Number.isSafeInteger(3123123123123123123122312312312);, which returns false.
The solution is to use Number.isInteger(); as your test instead of a regex. It correctly returns true for your large number.
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
Javascript can only store integers up to 9007199254740991 safely. Beyond that, javascript may convert the number to power notation (i.e., 5000000000000000000000 becomes 5e+21) or start converting digits into zeros for storage.
var n = 3123123123123123123122312312312;
console.log(n);
/* Output: 3.123123123123123e+30 */
You can use Number.isSafeInteger() to test whether the number is within the safe range, and then apply your original code to it in that case. If not, you can perform a different test (such as a test against /\d\.\d+e\+\d+ ) to see whether the decimal included is due to exponent notation.
Also be aware that a number in exponent notation will test true using Number.isInteger(), even if it was a floating point to begin with, as that information will be lost.
var x_int = 3123123123123123123122312312312;
var x_flt = 3123123123123123123122312312312.333333333;
console.log( x_int === x_flt);
/* Output: true */
console.log(Number.isInteger(x_flt));
/* Output: true */
I am trying to get an 8 decimal output from the following function.
The following function multiplies an input by 2 and then updates this input with the wagerUpdate variable. I would like this outputted number to have 8 decimal places.
For example: if input number is 0.00000001 (this code is for a bitcoin website), then I would like output number to be 0.00000002. For some reason the code below is not working properly as the output number is in the format of 2e-8 without the .toFixed(8) code. Please help if you are able to. Thank you so much.
<script>
function MultiplyWagerFunction() {
var wager = document.getElementById("wagerInputBox").value;
var wagerUpdate = wager*2;
document.getElementById("wagerInputBox").value = +wagerUpdate.toFixed(8);
}
</script>
If you remove the + before wagerUpdate.toFixed(8) it should work fine. wagerUpdate has already be converted to a number when you multiplied it by 2 so there should be no need for the unary +
var a = "0.00000001";
var b = a*2;
console.log(b.toFixed(8));
console.log(+b.toFixed(8));
^ see the difference.
The reason it doesn't work is because what you are doing is equivalent to:
+(b.toFixed(8))
because of the precedence of the operators (member access . is higher than unary +). You are converting b to a string with .toFixed and then converting it back into a number with + and then converting it back into a string again! (this time with the default toString behavior for numbers giving you exponential notation)
Just remove + from +wagerUpdate.toFixed(8); and you would be good.
Instead of:
document.getElementById("wagerInputBox").value = +wagerUpdate.toFixed(8);
try:
document.getElementById("wagerInputBox").innerHTML = +wagerUpdate.toFixed(8);
Why I say so is may be when you set value, browser tries to convert to best possible outcome. But, inner HTML should take the string equivalent!
i am getting answer 659 but that one is wrong answer please check it one's.
this is my code
var fact=1;
for(var i=1;i<=100;i++){
fact = fact*i;
}
var sum = 0;
while (fact > 0) {
sum += fact % 10;
fact = Math.floor(fact / 10);
}
console.log(sum);
There's a syntax error in the definition of length - the var keyword should come before it, not after it, and a similar problem in the calculation of sum.
Regardless, I think that converting the number to a string is the hard way to go at it. You can use the % operator to get the last digit and divide the number by 10 (don't forget to floor!) until you're done with all the digits:
var sum = 0;
while (fact > 0) {
sum += fact % 10;
fact = Math.floor(fact / 10);
}
Cool. You've written a very sensible piece of code. But there are a couple things to think about. One is the gigantic size of 100!. If you go to a console and enter some code, you'll see this:
> fact=1
1
> for(i=1;i<=100;i++){fact *= i;}
9.33262154439441e+157
Crikey. 10 to the 157. Look up the largest integer js can display. It's much smaller! So if this is a programming assignment, you have to be more subtle.
Next, if you get the number, all 158 digits, and you want to add them using your strategy, you may need to convert the strings you get (a substring is a string, after all) to a Number.
But really, the question is, can you determine the sum of the digits without calculating the number?
I'm building a plan file for XMPie Uplan. Javascript functions are allowed, so as I am learning JS, I thought I would take advantage of them. They seem friendlier than the proprietary QLingo functions. I don't think that should matter though, it is just JS. Anyone see a problem with my JS?
function cents(p) {
var monfor = toString(parseFloat(Math.round(p * 100) / 100).toFixed(2));
return monfor.slice(-2);
}
The purpose of this code is to return just the cents in a price.
Here is what is supposed to be going on:
First I make sure the number has two decimal places and convert to a string. Then I slice off the last two digits of my string leaving me with a 2 digit integer as a string which is the number of pennies in my price. This will flow into the cents portion of a price field with the cents in superscript. (I have another function that uses floor to kill the sub dollar part of the price that populates the dollar part of the price.) The error on this function is:
Error: cents: An error occurred while executing the function script.
Description: Object expected At line 2 character 0.
Thanks in advance for any help!
var numToParse = parseFloat(Math.round(p * 100) / 100).toFixed(2);
var monfor = numToParse.toString();
return monfor.slice(-2);
You were using the two string in the wrong way.
As mentioned in the comments, i didnt know this, you dont even need to parse it to string as the toFixed() parses it itself.
var monfor = parseFloat(Math.round(p * 100) / 100).toFixed(2);
return monfor.slice(-2);
I am using jQuery 1.7.2 and jQuery UI 1.9.1. I am using the code below within a slider. (http://jqueryui.com/slider/)
I have a function that should test two values and depending on the difference between the two values reformat them (to the appropriate decimal place). If the difference is greater than 10, I will parse out the integer. If the difference is greater than 5, it should keep one decimal. Everything else, I will keep two decimals.
When I enter two values that have a difference that is ten or less, I use the toFixed() function. And, in Firebug, I see an error:
TypeError: Low.toFixed is not a function
Low = Low.toFixed(2);
Is there something simple that I am doing wrong?
Here is my code:
var Low = $SliderValFrom.val(),
High = $SliderValTo.val();
// THE NUMBER IS VALID
if (isNaN(Low) == false && isNaN(High) == false) {
Diff = High - Low;
if (Diff > 10) {
Low = parseInt(Low);
High = parseInt(High);
} else if (Diff > 5) {
Low = Low.toFixed(1);
High = High.toFixed(1);
} else {
Low = Low.toFixed(2);
High = High.toFixed(2);
}
}
toFixed isn't a method of non-numeric variable types. In other words, Low and High can't be fixed because when you get the value of something in Javascript, it automatically is set to a string type. Using parseFloat() (or parseInt() with a radix, if it's an integer) will allow you to convert different variable types to numbers which will enable the toFixed() function to work.
var Low = parseFloat($SliderValFrom.val()),
High = parseFloat($SliderValTo.val());
That is because Low is a string.
.toFixed() only works with a number.
Try doing:
Low = parseFloat(Low).toFixed(..);
Low is a string.
.toFixed() only works with a number.
A simple way to overcome such problem is to use type coercion:
Low = (Low*1).toFixed(..);
The multiplication by 1 forces to code to convert the string to number and doesn't change the value.
You need convert to number type:
(+Low).toFixed(2)
parseFloat() will return NaN for empty string, why not using Number() function instead?
Values of other types can be converted to numbers using the Number() function.
parseFloat('').toFixed(2) // "NaN"
Number('').toFixed(2) // "0.00"
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
Low = Number(Low).toFixed(1);
add the Number function to convert Low into a number.
In a function, use as
render: function (args) {
if (args.value != 0)
return (parseFloat(args.value).toFixed(2));
},