Rounding up

javascript

Page 1 of 1

10 Replies - 38460 Views - Last Post: 28 April 2006 - 08:16 AM

#1 the_hangman  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 602
  • Joined: 18-January 06

Rounding up

Posted 26 April 2006 - 01:56 PM

Ok I want to write a device that always rounds a number up after dividing by 100 (javascript). My theory was to divide (%) the number by 1 and if the remainder was less than .49 and greater than 0, then it would round the number divided (/) by 100 and add one. Else, it would just divide the number by 100 and round..

Well something isn't working right. Here's my code. Did I miss something?

<html>
<head>
	<title>Test for rounding up</title>
<script language="javascript">

function adder()
{
  var abc == Number(data.value)

  if (abc % 1 <.50 && abc % 1 > 0)
    { result.value == round(abc / 100) + 1 }
  else
    { result.value == round(abc / 100) }
}

</script>
</head>
<body>

  <input name="data" onchange="adder();"><br />
  <input name="result" readOnly><br />

</body>
</html>


Is This A Good Question/Topic? 0
  • +

Replies To: Rounding up

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1876
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Rounding up

Posted 26 April 2006 - 02:07 PM

There is a much simpler way, Javascript has a built in Ceil() function in the math library:

Math.ceil(a)    // integer closest to a and not less than a


Just replace a with your decimal value.
Was This Post Helpful? 0
  • +
  • -

#3 the_hangman  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 602
  • Joined: 18-January 06

Re: Rounding up

Posted 26 April 2006 - 02:09 PM

thank you, i'll try that..
but i still want to try and get the other one to work right so i know what i did wrong
Was This Post Helpful? 0
  • +
  • -

#4 the_hangman  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 602
  • Joined: 18-January 06

Re: Rounding up

Posted 26 April 2006 - 02:18 PM

This isnt working either...

<html>
<head>
	<title>Test for rounding up</title>

<script type="text/javascript">

function testEr()
{
result.value == Math.ceil(data.value/100)}
}

</script>
</head>
<body>

  <input name="data" onchange="testEr();"><br />
  <input name="result" readOnly><br />

</body>
</html>

Was This Post Helpful? 0
  • +
  • -

#5 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1876
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Rounding up

Posted 26 April 2006 - 02:26 PM

You've got some issues in your code there...

result.value == Math.ceil(... The == is used for comparison, while the = is used for assignment.

At the end of the result.value = Math.ceil(data.value/100)} you have a bracket, this should be a semi-colon.

You'll also get errors about undefined object because you don't have your inputs in a form.

Here is the revised code that will work:
<html>
<head>
<title>Test for rounding up</title>

<script type="text/javascript">

function testEr()
{
stuff.result.value = Math.ceil(stuff.data.value);
}

</script>
</head>
<body>
<form name="stuff">
 <input name="data" onchange="testEr();"><br />
 <input name="result" readOnly><br />
</form>
</body>
</html>

Was This Post Helpful? 0
  • +
  • -

#6 the_hangman  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 602
  • Joined: 18-January 06

Re: Rounding up

Posted 26 April 2006 - 02:33 PM

*blushes*

wow..

it works now, but before you think I'm an idiot, I had surgery on my mouth yesterday, so i'm a little hopped up on painkillers (I can barely see straight). Normally, I'm pretty meticulous about syntax

Sorry
Was This Post Helpful? 0
  • +
  • -

#7 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: Rounding up

Posted 26 April 2006 - 03:41 PM

It happens to the best of us.
Was This Post Helpful? 0
  • +
  • -

#8 the_hangman  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 602
  • Joined: 18-January 06

Re: Rounding up

Posted 27 April 2006 - 08:46 AM

Just for the record, i feel a little better today and i figured out what was wrong before.

1.) I had round() instead of Math.round
2.) In the if statement I had if it was less than .50 and a remainder would be returned as a whole number.
3.) I was dividing by 1 and not 100

here is a little snippet i put together to fix it and show ways to round up. the first function is how I would assume the backend of programming works a ceiling or floor function

<html>
<head>
	<title>Examples for rounding up</title>
<script language="javascript">

<!-- This is how I assume the following two functions are determined on the back end of programming //-->
function rounder1()
{
 var abc = Number(data.value)

 if (abc % 100 < 50 && abc % 100 > 0)
   { result1.value = Math.round(abc / 100) + 1; }
 else
   { result1.value = Math.round(abc / 100); }
}

<!-- This uses the ceiling function to always round the number up to an integer //-->
function rounder2()
{
result2.value = Math.ceil(data.value / 100);
}

<!-- This uses the floor function to always round the number down to an integer and add one //-->
function rounder3()
{
result3.value = Math.floor(data.value / 100) + 1;
}

</script>
</head>
<body>

  Enter Data Here:<br />
  <input name="data" onchange="rounder1(); rounder2(); rounder3();" />
  <input name="calculate" type="submit" value="Calculate" />
  <br /><br />

  Results Here: <font style="font-size:10px;">These should all be the same</font><br />
  <input name="result1" readOnly /><br />
  <input name="result2" readOnly /><br />
  <input name="result3" readOnly /><br />

</body>
</html>

Was This Post Helpful? 0
  • +
  • -

#9 Arbitrator  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 51
  • View blog
  • Posts: 530
  • Joined: 26-January 05

Re: Rounding up

Posted 27 April 2006 - 03:31 PM

Just want to add some comments:
a. % specifically is modulus not division
b. to elaborate, == returns a true/false value hence "comparison"
c. based on your syntax (closing slashes) you're using XHTML so the readonly empty attribute must be spelled out as readonly="readonly"; onchange is invalid as all XHTML attribute names must be in lowercase, JS events notwithstanding; and the font tags (use span) and language attributes are deprecated.
Was This Post Helpful? 0
  • +
  • -

#10 the_hangman  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 602
  • Joined: 18-January 06

Re: Rounding up

Posted 27 April 2006 - 05:48 PM

thanks for clearing that up


i slack on the xhtml thing for some reason.. i just started doing that as standard from documents that used xhtml and when i just write plain html i haitually do it.. dunno...

the code still works though lol
Was This Post Helpful? 0
  • +
  • -

#11 Arbitrator  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 51
  • View blog
  • Posts: 530
  • Joined: 26-January 05

Re: Rounding up

Posted 28 April 2006 - 08:16 AM

the_hangman, on 27 Apr, 2006 - 04:40 PM, said:

the code still works though lol

That's what people who code for that disaster called Internet Explorer say. :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1