Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Rounding down in Basic

Status
Not open for further replies.
I'm working on some code (man has it been a work in progress) whereby a user can scroll through his GPS position and see it in Long/Lat, Universal Traverse Mercator, and Military Grid Reference System.

I was hung up on how to get the digraph letters for the MGRS, but I understand how to do that.

The problem is on the coding side.
The UTM easting is in float format because of all the trig I have to do to derive it. One of the things that I have to do to get the digraph is to divide the easting or northing by 100,000. When I divide the floating point easting by 100,000 and store it into a DWORD (32 bit unsigned variable) I'm noticing that it is rounding either up or down. I need it to ALWAYS round down.

I could convert the float to a string and then use the LEFT$ function to get the leading integer, but this seems unnecessary.

Any tips on this one? I'm using Amicus Basic, which is just the free limited version of Proton Basic.

Code:
dim TEMP_DWORD as DWORD
dim LONG_DIGRAPH as DWORD
dim EASTING as FLOAT
.
.
more declarations and more code
.
.


'EXTRACT THE LONGITUDINAL DIGRAPH FOR MGRS
TEMP_DWORD = LONG_ZONE - 1         'subtract one from the long zone variable and store in dword long_digraph
TEMP_DWORD = (TEMP_DWORD // 3)
TEMP_DWORD = TEMP_DWORD * 8     'take the 3 modulus of this, multiply by 8
LONG_DIGRAPH = EASTING / 100000 
LONG_DIGRAPH = LONG_DIGRAPH + TEMP_DWORD 'divide easting by 100,000 and then add value in temp_dword

Thanks!
 
Last edited:
If you subtract 0.5 before you convert to DWORD then you should get the rounded down number.

Mike.
 
Thanks Mike. It's pretty embarrassing actually that I didn't think of something so simple!

I ended up converting the entire thing to a string and pulling the first element out of the string array and then converting it back to decimal. That was a waste!

Thanks again.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top