Pages: [1]   Go Down
 Author Topic: Rounding a variable to the nearest quarter  (Read 2366 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 10
 « on: July 27, 2012, 08:52:35 pm » Bigger Smaller Reset

So I'm working on a project that includes reading out a distance from a Ping))) sensor to a 16x2 lcd.  I have the prototype done on my board and the code works brilliantly... a little too brilliantly.  Using the double variable the lcd is displaying the inches rounded to the nearest 100th, however I want the lcd to display to the nearest quarter inch instead.  For example; if ping.inches() returns 3.81 inches, I want it to display 4 inches instead by rounding up to the nearest .25.

Getting it to round to the nearest whole number is easy (replace "double" with "int"), but that's not accurate enough...

Any guidance on how this can be accomplished would be much appreciated!

Here is the code too in case you wanna check it out:
Code:
#include <Ping.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,5,4,3,2);
const int buttonPin = 8;
Ping ping = Ping(7,0,0);
double tareDist;
double constDist;
double curDist;
int val = 0;

void setup() {
pinMode(buttonPin, INPUT);
lcd.begin(16,2);
ping.fire();
constDist = ping.inches();
tareTheDist();
}

void loop() {
lcd.setCursor(0,0);
lcd.print("Patient's Ht:   ");
lcd.setCursor(0,1);
if (val == HIGH){
tareTheDist();
}
ping.fire();
constDist = ping.inches();
lcd.print(tareDist-constDist);
lcd.print(" inches     ");
delay(500);
val == LOW;
}

void tareTheDist(){
tareDist = constDist;
lcd.setCursor(0,0);
lcd.print("------TARE------");
lcd.setCursor(0,1);
delay(1000);
}
 Logged

Offline
Full Member
Karma: 0
Posts: 246
Code Monkey
 « Reply #1 on: July 27, 2012, 09:08:28 pm » Bigger Smaller Reset

So I'm working on a project that includes reading out a distance from a Ping))) sensor to a 16x2 lcd.  I have the prototype done on my board and the code works brilliantly... a little too brilliantly.  Using the double variable the lcd is displaying the inches rounded to the nearest 100th, however I want the lcd to display to the nearest quarter inch instead.  For example; if ping.inches() returns 3.81 inches, I want it to display 4 inches instead by rounding up to the nearest .25.

Getting it to round to the nearest whole number is easy (replace "double" with "int"), but that's not accurate enough...

Any guidance on how this can be accomplished would be much appreciated!

I think this is what you are looking for: http://en.wikipedia.org/wiki/Rounding#Rounding_to_a_specified_increment
 Logged

I yield() for co-routines.

Massachusetts, USA
Offline
Tesla Member
Karma: 201
Posts: 8661
 « Reply #2 on: July 27, 2012, 09:36:49 pm » Bigger Smaller Reset

Code:
lcd.print((int)(((tareDist-constDist)*4.0)+0.5)/4.0);

Multiply by 4.
Add 0.5 and truncate to get to the nearest integer.
Divide by 4.0.

Alternatively you could add 0.125, multiply by 4, truncate, and divide by 4.0.  Same effect.
 Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline
Newbie
Karma: 0
Posts: 10
 « Reply #3 on: July 27, 2012, 10:16:34 pm » Bigger Smaller Reset

Wow... math is amazing!
 Logged

Anaheim CA.
Offline
Karma: 46
Posts: 2865
...
 « Reply #4 on: July 27, 2012, 10:31:59 pm » Bigger Smaller Reset

I do a lot of monkey math. Math was never my strong suite, I learned what I needed for the work I did... however If I could buy a box of math now... I'd Buy a case...

Doc
 Logged

--> WA7EMS <--
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Pittsburgh, PA, USA
Offline
Karma: 91
Posts: 4696
I learn a bit every time I visit the forum.
 « Reply #5 on: July 27, 2012, 10:43:58 pm » Bigger Smaller Reset

Start by getting a pre-algebra school book (even an old one), read the chapters and -do the exercises-. By the time you're done you'll understand a whole lot about math in -usable terms- and things like division ("the relational operator") will never seem the same. It might be all you need.

 Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Massachusetts, USA
Offline
Tesla Member
Karma: 201
Posts: 8661
 « Reply #6 on: July 28, 2012, 05:59:54 am » Bigger Smaller Reset

If I could buy a box of math now... I'd Buy a case...

Doc

Why buy a case when you can get the warehouse-ful for free?