This question already has answers here:
JavaScript: Format whole numbers using toLocaleString()
(4 answers)
Closed 1 year ago.
I am creating a calculator and need guidance on adding thousand separators to a whole number.
//logic
var portVal = 100000000;
var rateUSD = 1.3654;
var sustPortBase = 22;
var avgPortBase = 66;
var sustPortCarb = (portVal*rateUSD/1000000)*sustPortBase;
var avgPortCarb = (portVal*rateUSD/1000000)*avgPortBase;
var diff = avgPortCarb - sustPortCarb;
var nbHomes = diff * 0.115;
var nbCars = diff * 0.216;
var nbTrees = diff * 16.5;
var nbPlanes = diff * 0.833359;
var avgPortCarbRound = avgPortCarb.toFixed();
var sustPortCarbRound = sustPortCarb.toFixed();
console.log("Cars:"+(nbCars).toFixed());
console.log("Cars Decimals:"+(nbCars).toLocaleString('en'))
Outputs:
Cars:1298
Cars Decimals:1,297.676
I would like to keep only thousand seperators but nothing after the (dot.) i.e. 1,297
https://jsfiddle.net/ge5oj2xk/1/
Try using Math.floor function.
console.log("Cars Decimals:"+Math.floor(nbCars).toLocaleString('en'))
Related
This question already has answers here:
Adding two numbers concatenates them instead of calculating the sum
(24 answers)
Closed last year.
I'm learning JavaScript and I wanted to accept user input and add a number to it ,but instead of outputting 12+12 = 24 it's outputting 12+ 12 = 1212.
document.getElementById("mybutton").onclick = function(){
var myAge = document.getElementById("mytext").value + 12;
document.getElementById("value").innerHTML = myAge;
}
I tried doing:
myName = Number(myName)
but it didn't work
In this case use a function parseInt()
example:
document.getElementById("mybutton").onclick = function(){
var myAge = parseInt(document.getElementById("mytext").value) + 12;
document.getElementById("value").innerHTML = myAge;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to make some calculations between inputs , I have 7 inputs and what I want to do is making calculations between them , so this is my code
$('.empCosts').on('input', function () {
var perHour = parseInt($('#per_hour').val());
var hours = parseInt($('#hour').val());
var perDay = parseInt($('#per_day').val());
var days = parseInt($('#day').val());
var perMonth = parseInt($('#per_month').val());
var month = parseInt($('#months').val());
var perYear = parseInt($('#per_year').val());
var perdayVal = parseInt(perHour * hours);
var perMonthVal = parseInt(perDay * days);
var perYearVal = parseInt(perMonth * month);
var perHourVal = parseInt(perDay / hours);
var perMonthDivide = parseInt(perYearVal / month);
$("#per_day").val(perdayVal);
$("#per_month").val(perMonthVal);
$("#per_year").val(perYearVal);
$("#per_hour").val(perHourVal);
if($(this).is(":focus")){
alert('sdfasdfad');
}
});
these inputs should be connected either you multiply or divide , when I try to change per_day value it doesn't change because I already set the value of that input with var perdayVal = parseInt(perHour * hours);, I want either you increase the number or decrease it , it should make the calculation either multiplying or dividing the inputs, here you have the fiddle
DEMO
try this https://jsfiddle.net/2jb2e3Lv/7/
$('.empCosts').on('input', function () {
var perHour = parseInt($('#per_hour').val());
var hours = parseInt($('#hour').val());
var perDay = parseInt($('#per_day').val());
var days = parseInt($('#day').val());
var perMonth = parseInt($('#per_month').val());
var month = parseInt($('#months').val());
var perYear = parseInt($('#per_year').val());
var perdayVal = perHour * hours;
var perMonthVal = perDay * days;
var perYearVal = perMonth * month;
$(this).attr("id") !== "per_day" && $("#per_day").val(perdayVal);
$(this).attr("id") !== "per_month" && $("#per_month").val(perMonthVal);
$(this).attr("id") !== "per_year" && $("#per_year").val(perYearVal);
});
This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 6 years ago.
I am trying to make a calculator that calculates the amount of calories that have been burned in a certain amount of time, but whenever I run it, I get 0 as the output. I have tried setting the calorieCountOut variable to an arbitrary number, and then it works fine, but every time I run it with this code here, I get 0. Here is my code:
const AGECONST = 0.2017;
const WEIGHTCONST = 0.09036;
const HRCONST = 0.6309;
const SUBTRACTCONST = 55.0969;
const TIMECONST = 4.184;
//var gender = document.getElementById("gender").innerHTML;
var gender = "male";
var weight = document.getElementById("weight");
var age = document.getElementById("age");
var time = document.getElementById("time");
var hr = 140;//dummy number
function calculate(){
if (gender = "male"){
var calorieCount = ((age * AGECONST) - (weight * WEIGHTCONST) + (hr * HRCONST) - SUBTRACTCONST) * time / TIMECONST;
}
//else if (gender = "female"){
//}
var calorieCountOut = calorieCount.toString();
document.getElementById("output").innerHTML = calorieCountOut;
}
Try getElementById('myId').value for the value inside the control instead of the control itself as an object.
Right now you assign a html object to a variable, but what you want (i assume) is the number stored in that html object.
I have a simple form, with 5 textboxes and 3 answers (also textboxes). The form calculates a result for the user with number inputs. My problem is my calculation does not work in IE, but works fine in both Chrome and Firefox.
What's wrong?
Here is my function:
function addNumbers()
{
var val1 = Number(document.getElementById("value1").value);
var val2 = Number(document.getElementById("value2").value);
var val3 = Number(document.getElementById("value3").value);
var val4 = Number(document.getElementById("value4").value);
var val5 = Number(document.getElementById("value5").value);
var val6 = '100';
var ansD1 = document.getElementById("answer1");
ansD1.value = Number((val1 * val2) * (val4 / val6));
var ansD2 = document.getElementById("answer2");
ansD2.value = Number((val1 * val3) * (val5 / val6));
var ansD3 = document.getElementById("answer3");
ansD3.value = Number (ansD1.value - ansD2.value);
}
Change this line:
var val6 = '100';
to this:
var val6 = 100;
You want all your values to be actual numbers (not strings) so you can do math on them.
Also, you don't need the Number() in these lines because the result of the numeric math is already a number. Plus the assignment to the answer fields is just going to convert the result to a string anyway:
ansD1.value = Number((val1 * val2)*(val4/val6));
They can just be this:
ansD1.value = (val1 * val2)*(val4/val6);
The modified code works fine in IE here: http://jsfiddle.net/jfriend00/5WFRA/.
Instead of Number use parseInt, otherwise they are treated as strings
Not the biggest whiz on math here... having a hard time figuring this out.
So I get the clients time of day using:
var clientdate = new Date();
var clientTime = clientdate.getHours() + 1;
Psuedo:
If clientTime() = 1, rgb should equal 55,91,128
If clientTime() = 24, rgb should equal 0,30,61
What I am trying to figure out is how to get the values to be correct if the time is, for instance, 14.
My original theory was this:
Subtract difference from RGB values.
So they would be 55,61,67.
Divide clientTime by 24
(clientTime/24), multiple that by
my difference in step 1.
Subtract step 2 from the original RGB values.
Here is the code:
var clientdate = new Date();
var clientTime = clientdate.getHours() + 1;
var r = 55;
var g = 91;
var b = 128;
var rn = 0;
var gn = 30;
var bn = 61;
var rt = (r-rn)*(clientTime/24);
var gt = (g-gn)*(clientTime/24);
var bt = (b-bn)*(clientTime/24);
var rf = r-rt;
var gf = g-gt;
var bf = b-bt;
Question
How do I get RGB values between 55,91,128 and 0,30,61 based on the time of day.
Did I do this correctly, and if so, how do I do it more concise?
If you want to be inclusive of the bounds on your color, then change clientTime to:
clientdate.getHours();
...and change rt, gt, and bt to:
var rt = (r-rn)*(clientTime/23.0);
var gt = (g-gn)*(clientTime/23.0);
var bt = (b-bn)*(clientTime/23.0);