Go Down

### Topic: small problem with float (Read 5101 times)previous topic - next topic

#### louish

##### May 28, 2012, 09:09 pm
hii all ... if i'm gettinig afloat values lets say 34.789854 and i want only 34.78 how it could be done ?

#### el_supremo

#1
##### May 28, 2012, 09:14 pm
I presume you mean when you print it, in which case this little sketch will print 34.13 on the serial monitor.
Code: [Select]
`void setup(void){  float a = 34.125456;  Serial.begin(9600);  Serial.println(a,2);}void loop(void){}`

Pete
Don't send me technical questions via Private Message.

#### louish

#2
##### May 28, 2012, 09:18 pm
no ... i want to save it in adeffrent variable ... so i can use it in math. equation after that .

#### cmiyc

#3
##### May 28, 2012, 09:21 pm

no ... i want to save it in adeffrent variable ... so i can use it in math. equation after that .

Why does it matter for the calculation?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

#### majenko

#4
##### May 28, 2012, 09:23 pm
Code: [Select]
`float source = 34.125456;int temp;float destination;temp = source * 100;destination = (float)source / 100.0;`

or

Code: [Select]
`float source = 34.125456;float destination;destination = floor(source*100.0)/100.0;`

#### louish

#5
##### May 28, 2012, 09:31 pm
okk here is the problem : my project  mesures distance between 2 points with gps and xbee , so far im able to collect the latitude and longtitude for the points in 4 variable , but the the coordinates that im getting from the gps changes rapidly after 3 points in both long and lat and that effects the result so badly. so what i need is to get numbers in this format for example :

lat1 : 32.11 , lon1 : 34.8717
lat2 : 32.11 , lon2 : 34.8716

#### dkl65

#6
##### May 28, 2012, 09:32 pm
Or better yet, make a function macro to round any number to any decimal point. Put this at the top of your sketch:
Code: [Select]
`#define roundTo(a,b) round(a*(1/b))/(1/b)`
Here are some examples:
Code: [Select]
`float num1 = roundTo(34.789854, 0.01); //returns 34.79byte num2 = roundTo(34.789854, 10); //returns 30float num3 = roundTo(34.789854, 0.1); //returns 34.8`
If you want to drop the extra numbers instead of rounding them, change round to floor.

#### louish

#7
##### May 28, 2012, 09:36 pm
noooo i can't round at all every number is extreemly important for distance calculations .

#### majenko

#8
##### May 28, 2012, 09:39 pm
If you want to get a more "stable" value, you can get a whole bunch of readings, and average them.

#### louish

#9
##### May 28, 2012, 09:50 pm
how it can be done ?

#### Fletcher_Chr

#10
##### May 28, 2012, 09:52 pm
Hi louish

I think you are doing it wrong - you should keep the high resolution numbers as long as possible.

Recalculate the distance even if the responce from the gps is noisy and then you should decide if the new distance has changed enough -or you could make a running average.

-Fletcher

#### dkl65

#11
##### May 28, 2012, 09:57 pm
Look at the Smoothing example, and use floats or doubles instead of ints.

#### AWOL

#12
##### May 28, 2012, 10:02 pm
I wouldn't bother with doubles; they're overrated on the Arduino
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### majenko

#13
##### May 28, 2012, 10:20 pm
Double == float on arduino.

#### louish

#14
##### May 28, 2012, 10:31 pm
i don't think smoothing is the solution for my problem because the gps transmitting NEMA protocol via serial communication in pin 2,3 . from the protocol im only interested in position and i've no way to get the position exept using this command : gps.f_get_position(&lat11, &lon11);
and this comand doesn't give me any choice to control the numbers i get so im storing the data in deferent variables to get the numbers as i want and its not workinig so far /]

Go Up