# Rounding up

Page 1 of 1

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

### #1 the_hangman

Reputation: 14
• 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>
<title>Test for rounding up</title>
<script language="javascript">

{
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>
<body>

</body>
</html>
```

Is This A Good Question/Topic? 0

## Replies To: Rounding up

### #2 skyhawk133

Reputation: 1900
• Posts: 20,332
• 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.

### #3 the_hangman

Reputation: 14
• 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

### #4 the_hangman

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

## Re: Rounding up

Posted 26 April 2006 - 02:18 PM

This isnt working either...

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

<script type="text/javascript">

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

</script>
<body>

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

</body>
</html>
```

### #5 skyhawk133

Reputation: 1900
• Posts: 20,332
• 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>
<title>Test for rounding up</title>

<script type="text/javascript">

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

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

### #6 the_hangman

Reputation: 14
• 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

### #7 snoj

• Married Life

Reputation: 86
• Posts: 3,569
• Joined: 31-March 03

## Re: Rounding up

Posted 26 April 2006 - 03:41 PM

It happens to the best of us.

### #8 the_hangman

Reputation: 14
• 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.

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>
<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>
<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 />

</body>
</html>
```

### #9 Arbitrator

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

## Re: Rounding up

Posted 27 April 2006 - 03:31 PM

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.

### #10 the_hangman

Reputation: 14
• 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

### #11 Arbitrator

Reputation: 51
• 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.