11 Replies - 17863 Views - Last Post: 19 November 2010 - 09:29 AM Rate Topic: -----

#1 Roblobob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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! :D

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  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Roblobob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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. :)
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • 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...
Was This Post Helpful? 0
  • +
  • -

#5 Roblobob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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. :D

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. :P

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.
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • 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.
Was This Post Helpful? 1
  • +
  • -

#7 Roblobob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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! :D
Was This Post Helpful? 0
  • +
  • -

#8 Roblobob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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. :P

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

Thanks. :D

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

Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • 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

Was This Post Helpful? 0
  • +
  • -

#10 badams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#11 Guest_Chris*


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
Was This Post Helpful? 1

#12 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Force rounding UP of decimal numbers

Posted 19 November 2010 - 09:29 AM

Great necro. :D Short and to the point! I hope Google indexes it so other people can benefit.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1