How to modify an int32_t's upper and lower parts

Hi all

I need to edit the hole part and the decimal part of a value stored as an int32_t ( GPS coordinates actually)

In my sketch I display the values with (Using Mega 2560)

String Create_Int32_t_String(int32_t Num) {

  char buffer[32];

  sprintf(buffer, "%s%ld.%04ld", Num < 0 ? "-" : "", abs(Num) / 10000, abs(Num) % 10000);

  return String(buffer);

}

Which works really good

However I need the user to be able to change the GPS coordinates

So I have a function that includes

    case 1:

      int32_t Top_Part = abs(GPS_Display_Data.User_latitude) / 10000;

      int32_t Lower_Part = abs(GPS_Display_Data.User_latitude) % 10000;

      if (LCD_Cursor_Postion == 5) {  // changing the hole number part

        if (Increase == true) {

          Top_Part++;  // increase the upper part

          if (Top_Part > 90) { Top_Part = 90; }

        } else {

          Top_Part--;  // decrease the upper part

          if (Top_Part < -90) { Top_Part = -90; }

        }

      } else {  // changing the decimal number part

        if (Increase == true) {

          Lower_Part++;  // increase the upper part

        } else {

          Lower_Part--;  // decrease the lower part

        }

      }

      GPS_Display_Data.User_latitude = Top_Part + Lower_Part;

      Clear_LCD_Line(1);

      LCD_4_Line.print("Lat : ");

      LCD_4_Line.print(Create_Int32_t_String(GPS_Display_Data.User_latitude));  // display the GPS latitude after converting the new user latitude value to a string

      break; 

The problem I'm having is I do not seem to be able to work out how I do this correctly so I get a proper Latitude by adding the Top_Part to the Lower_Part-
ie - GPS_Display_Data.User_latitude = Top_Part + Lower_Part;

I know the problem is my lack of knowledge but if someone could point me in the right direction, but perhaps Ive got the hole function wrong

I would be very appreciative for any help given

Kind Regards Grant Brown

GPS_Display_Data.User_latitude = Top_Part *10000+ Lower_Part;

Thank you b707,

That seemed to work but could you please explain why it worked so I can learn

Thanks Grant Brown

Since the Top and Lower parts are calculated in this two lines of the function:

so my expression is just a reverse transform from top and lower parts to latitude

When you do foo x 10 you shift left whatever is in foo by 1 position - you add a 0 to the right of foo.

For example 42 x 10 = 420

When you do foo x 100 you shift left whatever is in foo by 2 positions - you add two 0 to the right of foo.

For example 42 x 100 = 4200

When you do foo x 1000 you shift left whatever is in foo by 3 positions - you add three 0 to the right of foo.

For example 42 x 1000 = 42000

You probably get the idea by now. Multiplying by a power of 10 adds as many 0 you had

Now if you take the resulting number and add sometime that is small enough to fit where the 0s are on the right, you don’t disturb the upper part, you just replace the 0s

For example 42 x 1000 + 123 = 42000 + 123 = 42123

This is what the math formula provided by @b707 does.


Side note - the other formulas with the division and modulo are doing the opposite