Skip to content
View in the app

A better way to browse. Learn more.

The AVSIM Community

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Rounding Up Plane Heading Degree Magnetic Var

Featured Replies

  • Commercial Member

plane_heading = PLANE_HEADING_DEGREES_MAGNETICvar.var_value.n;I'd like to round this value up.I'm finding that Heading value in the FSX default aircraft are being rounded up.So for example, in the default 737 the heading could read 250.But when I return plane heading as a string to see what it's doing, I'll get 249.So how do I round the value up?I tried using ROUND, but VS didn't like that too much.Cheers,

  • Moderator

Well, one way would be this simple approach...Multiply by 100 before and divide by 100 afterwards. plane_heading = floor(plane_heading*100.0 + 0.5) / 100.0

Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator
Well, one way would be this simple approach...Multiply by 100 before and divide by 100 afterwards. plane_heading = floor(plane_heading*100.0 + 0.5) / 100.0
What am I missing?
249.0 * 100.0 = 24900.0    24900.0 + 0.5 = 24900.5   floor(24900.5) = 24900.0   24900.0 / 100 .0 = 249.0

Gerry Howard

  • Moderator

More like "what did I miss..."plane_heading = (int)floor(plane_heading*100.0 + 0.5) / 100.0For it to work properly one must explicitly cast the float result to an integer... :(Or, implicitly cast in the swprintf string's format to %!3d

Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator
More like "what did I miss..."plane_heading = (int)floor(plane_heading*100.0 + 0.5) / 100.0For it to work properly one must explicitly cast the float result to an integer... :( Or, implicitly cast in the swprintf string's format to %!3d
I still think there's something missing.
249.0 * 100.0 = 24900.0   24900.0 + 0.5 = 24900.5   int(24900.5) = 24900   24900 / 100.0 = 249.0

The following seems to give the desired answerplane_heading = int((plane_heading + 5) / 10.0) * 10.0

249.0 + 5 = 255.0   255.0 / 1- = 25.5   int(25.5) = 25.0   25.0 * 10.0 = 250.0

I think the general expression to round a number n to the nearest multiple of m is:int((n + (m / 2.0)) / n) * mE&OE

Gerry Howard

Assuming plane_heading is a floating point variable, you can just use this:int rounded = (int)(plane_heading + 0.5);Tim

Assuming plane_heading is a floating point variable, you can just use this:int rounded = (int)(plane_heading + 0.5);Tim
That always rounds down
249.0 + 0.5 = 249.5int(249.5) = 249.0

Gerry Howard

Well, if your input value is 249.0, the output value should be 249, only input values equal or higher than 249.5 should round up to 250. If you want anything higher than 249.0 to be 250, then you can use (int)ceil(plane_heading).Tim

Maybe I'm misunderstanding, are you trying to round up to the 1's place, or the 10's place? I was assuming the 1's place, but if needing the 10's place, then the (int)((plane_heading + 5.0) / 10.0) * 10 version is correct :-> Tim

  • Moderator

The 1's place, Tim.Bryan is writing a GDI+ string, and any value even slightly less than a whole number is "rounding down," e.g. 359.9 displays as 359...

Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator
The 1's place, Tim.Bryan is writing a GDI+ string, and any value even slightly less than a whole number is "rounding down," e.g. 359.9 displays as 359...
Then I've misunderstood. Does he want to round up or to the nearest integer?

Gerry Howard

  • Moderator
Then I've misunderstood. Does he want to round up or to the nearest integer?
Nearest integer, whole number, or round up to whole number...As I stated, there are more than a few ways to accomplish the goal... :(

Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator
  • Commercial Member

Here's a method that actually follows the 'rule' of >= 5 for rounding:

FLOAT64 input_val; // the value you wish to roundFLOAT64 frac; // the fractional value, for this example... fractional value of 1'sFLOAT64 ret_val; // the returned valuefrac = fmod(input_val,1); // extract the fractional valueret_val = input_val - frac; // remove the fractional value from the return valueif (frac>=0.5){  ret_val += 1; // if greater or equal to 0.5 add 1 to return value}

Ed Wilson

Mindstar Aviation
My Playland - I69

Nearest integer, whole number, or round up to whole number...As I stated, there are more than a few ways to accomplish the goal... :(
To round(!) off this discussion the following general C expressions should round a number, n, to a multiple of a number m. The number m need not be whole number - It could be, say, 2.5.
	   Round to nearest	   (int)((n + (m / 2.0)) / m) * m	   	   Round up	   ((int)(n / m) + (fmod(n, m) > 0? 1: 0)) * m	   	   Round down	   (int)(n / m) * m

E&OE

Gerry Howard

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.