Having trouble building a calculator - javascript

I am trying to build a simple calculator to learn javascript/jquery but seem to be getting quite confused on how to make it work. Basically at the moment I dont seem to be able to update my tally correctly, instead of the numbers/values being added together in a sum they are being concatenated instead, if anyone could give me some guidance on how I can get back on the right path with this and any other advice on how to make the code more efficient then that would be great. I don't want anyone to give me the correct script just yet.
http://jsfiddle.net/kyllle/edRk9/4/
Thanks all in advance for your advice

The val() method returns a string, and the + operator will concatenate if one of the operands is a string. You can convert the value to a number using parseInt[MDN]:
$('input.num', '#buttons').on('click', function() {
// when click a number it appears in #result
currentNumber = parseInt($(this).val(), 10);
$('#result').html(currentNumber);
});
Note, for parsing decimal numbers, use parseFloat[MDN], not parseInt.

parseInt, parseFloat or multiply value by 1 before sum or use eval(), but it's the worst case :)

Related

Formatting a changing number

I would like to format an increasing number to make it have 2 digits after the decimal place at all times, but I can't seem to make it happen. I have tried using if statements to add 2 zeroes to the end of it, but this method doesn't work very well. Please may I have some help? Thank you!
My code:
if (round2(num)<floor(num)+0.001) {
document.getElementById('number').innerHTML='number
'+round2(num)+'.00';}
Another method that I have tried:
if (round2(num)<floor(num*10)/10+0.001) {
document.getElementById('number').innerHTML='number
'+round2(num)+'.0';}
var xNumber = 23.58841;
console.log(xNumber.toFixed(2));

Java Script Buffer,alloc(5,number.toString()) returns float

i was working with buffer in JavaScript. I want to write the buffer with first five digit of provided input. But instead of returning 23917 it return 2.391.Can someone explain why it does that?
Thank you so much in advance.
var number=23917397219379217392781;
var buffer=new Buffer.alloc(5,number.toString());
console.log(buffer.toString());
That's because for large numbers, toString() returns a scientific-notation string representation of the number.
In your case, number.toString() is '2.3917397219379218e+22'.
To get around this, implement your own numberToString method, and use that to get a non-scientific-notation string like '23917397219379217392781'

Adding a whole number and a decimal in Javascript results in removal of the decimal?

I'm sure this is simple, but in my javascript code, I have two numbers. One contains a decimal, and the other doesn't, and I add them together (ie. 7.5 + 5), I am getting a result with NO decimal value.
Do I need to cast each number variable as a double? I know that all numbers are doubles in javascript - which is why I do not understand this behavior...
For instance, I have var answer = week1 + week2;. Does this make sense?
Thanks in advance!
I am sorry for wasting time - turns out I was using parseInt instead of parseFloat to gather the "week" values I spoke about.
Can someone please close this question or delete it? Before the shame consumes me?

Trouble converting javascript string to number

I have a script assigns a variable using parseFloat as follows:
var vendorCost = parseFloat(vendorSearchresults[0].getValue('vendorcost')).toFixed(2);
I assumed this would make the variable a number. However when I check it with typeof - it reports it as a string. My solution is as follows:
vendorCost = parseFloat(vendorCost);
Which works, however I'm trying to be more efficient when coding and would like to understand why it doesn't make vendorCost a number when assigning it a number? Is there a way I could make the first statement make vendorCost a number without the need for the second statement? Thanks in advance.
Update - just thought I should mention I'm having the exact same issue without using .toFixed -
var vendorLandedCost = parseFloat(vendorSearchresults[0].getValue('custentity_asg_landed_cost','vendor'));
vendorLandedCost = parseFloat(vendorLandedCost);
The last toFixed() call converts the result of the first parseFloat into a string.
Looks like you need to round the number to two decimal places, which is why you're using the parseFloat call. You can do something like this instead:
vendor_cost = Math.round(parseFloat(vendorSearchresults[0].getValue('vendorcost')) * 100) / 100
Well, Number.toFixed returns string because it is a data presentation function.
See the docs.

d3.format wont keep trailing zero

I'm using d3.round(num,2) to round a number from say, 2.567 to 2.57. My problem is that when I do this, I want 2 decimal numbers of precision to be used at all times/regardless of if it is a zero.
when I have a number like 2.201, I want the display to be 2.20, and instead am coming up with 2.2. Is there any way to format the round function to always include zeroes?
Thanks for any help!
Edit: used num.toFixed(2) and that works, but I am returning d3.format("$")(num.toFixed(2)) and that is only returning $2.3, which is the reason I needed this, I am looking to display a price. If anyone has help to offer with that, it would be great, thank you
Try this:
d3.format("$.3n")(num);

Categories

Resources