Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-11-2010, 07:01 PM   PM User | #1
mickormick
New to the CF scene

 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
mickormick is an unknown quantity at this point
jQuery - Rounding Numbers

Wizards - I have a challenge!

I am writing a timecard app. The stipulatioin is all Hours fields can only be whole hours or half hour increments. For example, 1.0 is good, 1.5 is good, 1.2 is bad, 1.9 is bad.

I would like to have the field automatically change a value of 1.9 to a value of 2.0 (perhaps onKeyUp).

I would like to have the field automatically change a value of 3.2 to a value of 3.0.

In a nutshell - only allow whole and half hour values. Otherwise round up OR round down.

Thanks for any help you can provide. I am successfully using the Pengoworks plugin for calculations - absolutely priceless. If you think modifications can be made to it, plz let me know.
mickormick is offline   Reply With Quote
Old 11-11-2010, 07:58 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Here you are:-

Code:
Enter hours <input type = "text" id = "hours" size = "5" maxlength = "5" onkeyup ="chk()">

<script type = "text/javascript">
function chk() {
var val = document.getElementById("hours").value;
val = val.replace(/[^0-9\.]/gi, "");  // strip all but digits and decimal point
var valsplit = val.split(".");
valsplit[0] = parseInt(valsplit[0]);

if (valsplit[1]) {
valsplit[1] = valsplit[1].replace(/(\d)(.)/, '$1');  // only one digit after decimal point

if (valsplit[1] >5) {
valsplit[1] = 0;
valsplit[0] = valsplit[0] +1;
}

if (valsplit[1] <5) {
valsplit[1] = 0;
}

}

var newval = valsplit.join(".");
if (isNaN(newval)) {newval = ""}
document.getElementById("hours").value = newval;
}

</script>
Quizmaster: Name something a bald man does not have to buy.
Contestant: A razor.

Last edited by Philip M; 11-12-2010 at 08:02 AM.. Reason: Improvement
Philip M is offline   Reply With Quote
Old 11-11-2010, 08:03 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,220
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You can also do it mathematically.
Code:
<input name="hours" 
 onchange="this.value = 0.5 * Math.round(parseFloat(this.value)*2);"/>
This will show "NaN" if the user doesn't enter a valid number, so it doesn't have the protections of Philip's code.

Hmmm...I guess we could add to that:
Code:
<input name="hours" 
 onchange="this.value = 0.5 * Math.round(parseFloat(this.value)*2);
           if (this.value=='NaN') this.value=0;" />
Yeah, that works.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 11-11-2010, 08:11 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
Code:
<input name="hours" 
 onchange="this.value = 0.5 * Math.round(parseFloat(this.value)*2);
           if (this.value=='NaN') this.value=0;" />
Yeah, that works.
Oh no it doesn't! Your script merely rounds to the nearest .5.

I enter 12.3 and get 12.5, not 12.0 which is what the OP asked for. Likewise I enter 12.7 and get 12.5, not 13.0.

In any case the OP wanted onkeyup, not onchange.

With onkeyup I enter 12.9 and get 129.

Last edited by Philip M; 11-11-2010 at 08:21 PM..
Philip M is offline   Reply With Quote
Old 11-11-2010, 08:12 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,220
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Hmmm...that converts "3.2" to "3". No trailing ".0".

Okay...if that's important:
Code:
<input name="hours" 
 onchange="var n = 0.5 * Math.round(parseFloat(this.value)*2);
           this.value= isNaN(n) ? 0 : n.toFixed(1);" />
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 11-11-2010, 08:16 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,220
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Ahhh...granted.

But (a) he said "(perhaps onKeyUp)"

(b) onkeyup is a bad solution in my never overly humble opinion. What about the person who uses MOUSE ONLY to copy/paste a value into a form field? You will never get an onkeyup event. I'm not fanatic about it, but I very, very seldom use onkeyup in stuff I do.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 11-11-2010 at 08:19 PM..
Old Pedant is online now   Reply With Quote
Old 11-11-2010, 08:22 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,220
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I have to admit I like how your code gets around several possible pitfalls. Very clean.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
Philip M (11-11-2010)
Old 11-11-2010, 08:24 PM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
Hmmm...that converts "3.2" to "3". No trailing ".0".

Okay...if that's important:
Code:
<input name="hours" 
 onchange="var n = 0.5 * Math.round(parseFloat(this.value)*2);
           this.value= isNaN(n) ? 0 : n.toFixed(1);" />
Still wrong.
Enter 2.4 and get 2.5. Enter 2.6 and get 2.5. Suggest you read carefully what the OP wants.
Philip M is offline   Reply With Quote
Old 11-11-2010, 08:32 PM   PM User | #9
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
Ahhh...granted.

But (a) he said "(perhaps onKeyUp)"

(b) onkeyup is a bad solution in my never overly humble opinion. What about the person who uses MOUSE ONLY to copy/paste a value into a form field? You will never get an onkeyup event. I'm not fanatic about it, but I very, very seldom use onkeyup in stuff I do.
Well, simply make it read

Enter hours <input type = "text" id = "hours" size = "5" maxlength = "5" onkeyup ="chk()" onchange = "chk()" >
Philip M is offline   Reply With Quote
Old 11-11-2010, 08:51 PM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,220
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Yep. And as I said, yours is pretty nice in the way it handles idiotic entries. I'd still not worry about onkeyup on my own pages, but for anybody who wants to use it, your code is great.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 11-11-2010, 09:14 PM   PM User | #11
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
I'd suggest to pass the field reference to make the function reusable with other Hour fields that the original poster has.

Code:
<input type="text" id="hours" size="5" maxlength="5" onkeyup="chk(this);" onchange="chk(this);" />

<script type = "text/javascript">
function chk(objFld) {
  var val = objFld.value;

  ....

  var newval = valsplit.join(".");
  objFld.value = newval;
}
</script>
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 11-11-2010, 09:49 PM   PM User | #12
mickormick
New to the CF scene

 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
mickormick is an unknown quantity at this point
Thanks Gents

Now I have to humble myself, reveal my true lack of understanding, and ask for one more favor...

Why can't I get this function to work?

<html>
<head>
<script type="text/javascript" language="javascript">
function roundme(myhours){
var fhours = 0.5 * Math.round(parseFloat(myhours)*2);
fhours.toFixed(1);
}
</script>
</head>
<body>
<form name="form1" id="form1">
<input name="hours1" onkeyup="roundme(this.value)" value="0.0" size="2" /><br />
<input name="hours2" onkeyup="roundme(this.value)" value="0.0" size="2" /><br />
<input name="hours3" onkeyup="roundme(this.value)" value="0.0" size="2" /><br />
</form>
</body>
</html>
mickormick is offline   Reply With Quote
Old 11-11-2010, 10:13 PM   PM User | #13
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,220
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Ummm....you really should NOT be using my code if you are going to use onkeyup

As Philip correctly pointed out, it can't handle decimal points correctly.

But anyway, you are missing the *ASSIGNMENT* of the value back into the form field.
Code:
function roundme(myhours){
    var fhours = 0.5 * Math.round(parseFloat(myhours)*2);
    myhours.value = fhours.toFixed(1);
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 11-11-2010, 11:22 PM   PM User | #14
mickormick
New to the CF scene

 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
mickormick is an unknown quantity at this point
Thanks Again Wizards

You managed to come up with a solution within an hour! I have been struggling for days.

Thanks for the examples (solution).

-Dave
mickormick is offline   Reply With Quote
Old 11-12-2010, 01:09 AM   PM User | #15
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,220
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by mickormick View Post
You managed to come up with a solution within an hour!
I dunno about Philip, but my solution took about 2 minutes. <grin/>

(Of course, mine was the much easier one.)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Tags
numbers, rounding

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:11 PM.


Advertisement
Log in to turn off these ads.