# Force rounding UP of decimal numbers

Page 1 of 1

## 11 Replies - 19476 Views - Last Post: 19 November 2010 - 09:29 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=102840&amp;s=76703ca33fddcc097162835144a40faf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Roblobob

Reputation: 0
• Posts: 6
• Joined: 01-May 09

# Force rounding UP of decimal numbers

Posted 01 May 2009 - 03:42 PM

I need to know how to force the rounding UP of a decimal number even if the decimal is below x.5.

2.01 should become 3.00 for example.

Simple idea but I have no idea how to achieve this. Help!

I currently need this for a program I am writing in C# using SharpDevelop v3 but I will also be porting the program into Java and possibly Objective-C after that so if you know how to achieve the same result in those languages too I would be very grateful for your knowledge.

Thanks!

Is This A Good Question/Topic? 0

## Replies To: Force rounding UP of decimal numbers

### #2 KYA

• g++ jameson.cpp -o beverage

Reputation: 3140
• Posts: 19,176
• Joined: 14-September 07

## Re: Force rounding UP of decimal numbers

Posted 01 May 2009 - 03:52 PM

Assuming integers or even doubles, why not Math.floor(number) + 1?

Alternately, you can explore the Math.round() family of functions.

### #3 Roblobob

Reputation: 0
• Posts: 6
• Joined: 01-May 09

## Re: Force rounding UP of decimal numbers

Posted 01 May 2009 - 04:09 PM

Thank you for your post, KYA.

Just to clarify, (I am a noob at programming), this works by ALWAYS rounding down so long as the number is greater than a whole number, and then adds one?

Thanks.

### #4 KYA

• g++ jameson.cpp -o beverage

Reputation: 3140
• Posts: 19,176
• Joined: 14-September 07

## Re: Force rounding UP of decimal numbers

Posted 01 May 2009 - 04:41 PM

Yes it rounds down to the lowest applicable integer equivalent, i.e.

2.5 -> 2.0 +1 = 3.0
2.1-> 2.0 +1 = 3.0
2.9_2.0 +1 = 3.0
3.1->3.0+1 = 4.0

etc...

### #5 Roblobob

Reputation: 0
• Posts: 6
• Joined: 01-May 09

## Re: Force rounding UP of decimal numbers

Posted 01 May 2009 - 04:50 PM

Right, I get it. And now I am going to complicate matters.

When the number is whole to begin with, I don't want to touch it. That is I don't want the 1 to be added.

The 1 should ONLY be added in circumstances where the number has any decimal value.

For example...

1.9 becomes 1.0 + 1.0 = 2.0
1.7 becomes 1.0 + 1.0 = 2.0
1.4 becomes 1.0 + 1.0 = 2.0
1.1 becomes 1.0 + 1.0 = 2.0
1.0 stays unchanged
2.0 stays unchanged
3.0 stays unchanged

Is this possible? Thanks.

### #6 KYA

• g++ jameson.cpp -o beverage

Reputation: 3140
• Posts: 19,176
• Joined: 14-September 07

## Re: Force rounding UP of decimal numbers

Posted 01 May 2009 - 04:53 PM

Do a quick check:

```//if not a whole number
if(!(Math.floor(num) == num) //floor will give you the equivalent integer number
{
//do the floor stuff, add 1
}

```

That might be slightly off, writing in the fast reply box.

### #7 Roblobob

Reputation: 0
• Posts: 6
• Joined: 01-May 09

## Re: Force rounding UP of decimal numbers

Posted 01 May 2009 - 05:05 PM

```//if not a whole number
if(!(Math.Floor(num) != num)) //floor will give you the equivalent integer number
{
Math.Floor(num);
num=num+1;
};
```

This is looking good! I will do more testing and get back to you to confirm. Thank you very much!

### #8 Roblobob

Reputation: 0
• Posts: 6
• Joined: 01-May 09

## Re: Force rounding UP of decimal numbers

Posted 01 May 2009 - 05:33 PM

Okay, I thought I knew how your code worked and as a result I also thought that you had made an error in the if statement. I reasoned that you should test whether the numbers were different rather than the same. On further testing this was shown not to be the case. So now I am scratching my head wondering how it works.

Can you explain the if statement in detail, please.
```if(!(Math.Floor(div4) == div4)) //floor will give you the equivalent integer number
```

Thanks.

EDIT: Ahh! The "!" directly after the IF. That "!" turns the test into, "Is the following false"? I was confused by the double "=".

This post has been edited by Roblobob: 01 May 2009 - 05:45 PM

### #9 KYA

• g++ jameson.cpp -o beverage

Reputation: 3140
• Posts: 19,176
• Joined: 14-September 07

## Re: Force rounding UP of decimal numbers

Posted 01 May 2009 - 06:06 PM

Yeah we test for equivalency between the floor of a number and itself. If it is a whole number (2.0, 3.0, etc...) then the floor of it will be the same. We then want to look for cases where this is not (!) true, i.e. anything but a whole number.

edit:

A truth table for a whole number would be:

not true = false

For anything else:

not false = true

This post has been edited by KYA: 01 May 2009 - 06:07 PM

Reputation: 0
• Posts: 1
• Joined: 11-December 09

## Re: Force rounding UP of decimal numbers

Posted 11 December 2009 - 03:40 PM

I think you are looking for for the following method Math.Ceiling(decimal)

Returns the smallest integral value that is greater than or equal to the specified double-precision floating-point number.

Reputation:

## Re: Force rounding UP of decimal numbers

Posted 19 November 2010 - 09:26 AM

Yeah just use Math.Floor(number) to round down and Math.Ceiling(number) to round up. Using floor + 1 is not necessary and can result in unnecessary code

### #12 Sergio Tapia

• D.I.C Lover

Reputation: 1254
• Posts: 4,168
• Joined: 27-January 10

## Re: Force rounding UP of decimal numbers

Posted 19 November 2010 - 09:29 AM

Great necro. Short and to the point! I hope Google indexes it so other people can benefit.