I am trying to do a select box with 4 options
00,15,30,45
I want to take the current time and round it to 15 min increments, and have the value change.
I have
current_min = start_date.getMinutes();
$('#event-hour').val(current_min);
I played with this roundedMinutes=(15*Math.floor(enteredMinutes/15)) but i couldn't get it to work right.
Use Math.round instead of Math.floor and everything should be ok-- other than that, your equation for rounding to the nearest n is correct.
currentTimeRounded = (15*Math.round(date.getMinutes()/15));
js> (15*Math.round(date.getMinutes()/15));
15
Works just fine for me.
Related
I have a number that increases every second by addition. The problem is that it contains way too many decimals. An example of an output which I do not want would be something like 1.5999999999999999 or 1.600000000001. I have tried using the toFixed method, passing in 2 as it's value, and I am still getting the issue. I have also tried using the Math.round(number * 100) / 100 and am still getting uggly decimals.
How can I get an output that only contains a specified number of decimal points? I want to be able to choose how the number rounds. The function to round should be something like this:
function round(numberToRound, afterDecimals){
//Rounding code which takes into the account the number of decimal values I wish to display should go here
}
Thank you.
I am so sorry, I didn't call the function in the right place. The function works as expected, I just wasn't updating the innerHTML correctly.
Again, I am sorry for overlooking this.
I'm trying to make a stock chart, and were looking for a way to properly split the price on the x-axis and the date (in milliseconds) on the y-axis.
if I for example have 1000 dates, I can't show them all. But would like to show 10.
so if I have two dates 1266278400000 and 1477008000000 Is there some function in javascript that can find 10 evenly spread numbers between them?
The same goes for price, but I guess that's pretty much the same.
I think it's as simple as:
(high - low) / 10
That gets your step size. Then, loop through adding that step size each iteration.
So I've looked into this for several hours before finally giving up and asking help.
I'm currently trying to form fill a character sheet for Pathfinder (D&D 3.5 equivalent) using adobe acrobat. I want to make it so when I fill in my strength score it will auto fill out anything that has to do with strength.
More specifically I need it to take my ability score divide by two and subtract 5 for my ability modifier. But when I use 17 for instance as my Strength score my modifier is 4. I need it to round down not up.
I tried to subtract 5.5 instead and that works until its 10 or lower. At which point I have the opposite problem.
My current code is Strength/2-5
Use Math.floor() like this:
var score = 17.0;
result = Math.floor((score / 2) - 5);
alert(result)
Output:
3
Original:
Strength/2-5
(it worked but it needed to round down instead of up)
Final:
var a = this.getField("Strength")
event.value = Math.floor((a.value - 10) / 2)
Thank you for trying everybody! Process of elimination gets it done
Related to my previous question javascript math.round and math.floor work fine in IE and Opera but not Chrome, Safari or Firefox. Getting NaN
I have updated fiddle here http://jsfiddle.net/8VmPm/
The reason behind this script may seem a little cloudy so I'll try to explain. CDRs (Call detail records) from a certain network operator come in seconds for voice records and bytes for data records. I'm writing a "calculator" to convert seconds to minutes and bytes to megabytes.
I also took it a step further and added some example plans in there to check for overage.
My problem (which is simply cosmetic, but I am OCD like that) is in the first calculation (Math.round(minusage / 60)) (lines 22 and 23 in fiddle). I need it to round up to the next minute even if the value entered in the Usage Used field is over by 1 second.
Example:
In the Minute Which Plan? dropdown, pick Plan A (500 Minutes)
In the Usage Used field, enter in 30001 (which is 500 minutes and 1 second)
Click the Calculate button
Results in the Usage Summary field will be:
500 minutes used. -1 of 500 minutes remaining
Currently, it will not say, "501 minutes used" until the value entered in Usage Used field is 30030 or greater (for Plan A)
I need it to go to the next minute up even if over by 1 second (otherwise it'll confuse the non-techies who will be using it)
Any help with this matter would be greatly appreciated!
Take a look at Math.ceil(...). It is a JavaScript method for rounding up to the nearest integer. Here is a link to the MDN page.
I understand that JS math is not perfect. but how can i format this to get the correct answer as I have a cart item which costs .60 cents and they can change the quantity?
var a=3*.6;
document.write(a);
writes 1.7999999999999998
Obviously I want to write 1.8. any ideas how to accomplish this?
Use toFixed to round it back:
var a = 3*.6;
document.write(a.toFixed(2));
If you need it as a number, add a + sign before it:
var a = 3*.6;
console.log(+a.toFixed(2)); // Logs: 1.8, instead of "1.80"
var a=3*.6;
a = Math.round(a*10)/10;
document.write(a);
Since you want to round to the 10ths place, you need to multiply the number by 10, round the result of that multiplication to the nearest whole number, and then divide the result by 10.
It's not sexy, but ya gotta do whatchya gotta do.
var a=(3*(.6*100))/100;
document.write(a);
Example: http://jsfiddle.net/aJTJq/
multiply .6 by 100 to get the 60 cents
multiply that by 3
divide it by 100 to return it as a dollar figure
Write the .6 as a fraction: a=3*6/10 and you get 1.8
As a more general rule, you could try rounding to the nearest millionth with
Math.round(result*1000000)/100000 and seeing what that gets you.
What you want to do is have some type of rounding function.
Try this:
Rounding Function
<script language="javascript" type="text/javascript">
function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
document.roundform.numberfield.value = parseFloat(newnumber); // Output the result to the form field (change for your purposes)
}
</script>
And then something like this to call the function
<form name="roundform">
<input type="text" name="numberfield" value="">
<input type="button" value="Round" onClick="roundNumber(numberfield.value, 2);">
</form>
This example just takes a number in the text field and ensures that it is rounded to two decimal places.
This was taken from http://www.mediacollege.com/internet/javascript/number/round.html
There are more examples on this link as well. Hopefully this helps.
Cheers