Good day all. I've had some success using floating point number calculations with my ranging module distance reader. The problem is that when I need to output the calculated value through the RS232 port it is my understanding that I need to convert it to a string. Looking at sourceboost documentation I don't see a method to convert Float to string. I have to convert float to int then int to string. The problem is that the conversion to int from float get rounded and the numbers after the decimal point get cut out. I want to measure to 1/8th(.125) of an inch which would give values to 3 places after the decimal point. Is this possible on a PIC. Here is a clip of the code I'm using. It works. It rounds to the nearest inch. Any Ideas on this?
Ondra
tmrcnt = float32_from_int32(FCV_T1_COUNT);
cal1 = float32_mul(tmrcnt,13.02);
cal1 = float32_div(cal1,9.21825); //gives 1/8 inch count
cal1 = float32_div(cal1,2);
cal1 = float32_div(cal1,8); //gives inch whole number
FCV_SENS1 = float32_to_int32(cal1);
Converting float to string
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Converting float to string
Hello Ondra
You can do something similar to the following.
Int_Var = Float_var //Assign whole part of float variable to int_var
Float_var = Float_var - Int_Var //Subtract whole number potion of float from float variable
Float_var = Float_var * 1000 //Multiply real portion of float var by 1000
Dec_Point_Int_Var = Float_var //Read the rest of the float variable after the decimal point into seperate variable
Then to print out the variables
send Int_Var
send decimal point "."
send Dec_Point_Int_Var
You can do something similar to the following.
Int_Var = Float_var //Assign whole part of float variable to int_var
Float_var = Float_var - Int_Var //Subtract whole number potion of float from float variable
Float_var = Float_var * 1000 //Multiply real portion of float var by 1000
Dec_Point_Int_Var = Float_var //Read the rest of the float variable after the decimal point into seperate variable
Then to print out the variables
send Int_Var
send decimal point "."
send Dec_Point_Int_Var
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: Converting float to string
Wow, that's good, and makes sense.
Some of these things, I guess you'll only see with experience.
Thanks Ben
Ondra
Some of these things, I guess you'll only see with experience.
Thanks Ben
Ondra
-
- Posts: 45
- Joined: Mon Dec 22, 2008 11:22 pm
- Location: Prospect, NS, Canada
- Has thanked: 7 times
- Been thanked: 2 times
Re: Converting float to string
Hello Ben,
I have a question regarding this float calculation and printing this out.
I have used the C code float calculation and found a small problem.
When performing the following line:
Float_var = Float_var - Int_Var //Subtract whole number potion of float from float variable
the calculation might end up as 0.035
when you multiply this by 1000 the outcome is 35 and the printout will show 35 instead of 035. This is a problem when for instance your reading a voltage. (3.35 versus 3.035)
I'm sure there is a solution to it but my knowledge on C code is 0, so it is extremely difficult to figur it out.
Thanks for your great support!!
Best regards
Henk
I have a question regarding this float calculation and printing this out.
I have used the C code float calculation and found a small problem.
When performing the following line:
Float_var = Float_var - Int_Var //Subtract whole number potion of float from float variable
the calculation might end up as 0.035
when you multiply this by 1000 the outcome is 35 and the printout will show 35 instead of 035. This is a problem when for instance your reading a voltage. (3.35 versus 3.035)
I'm sure there is a solution to it but my knowledge on C code is 0, so it is extremely difficult to figur it out.
Thanks for your great support!!
Best regards
Henk
-
- Valued Contributor
- Posts: 548
- Joined: Tue Jun 26, 2007 11:23 am
- Has thanked: 6 times
- Been thanked: 44 times
Re: Converting float to string
Hello Henk,
When you multiply the fractional part of the floating point number by 1000 you need to test the result to determine the number of padding zeros to be inserted.
// 3-digit value - print directly
Is result >= 100?
Yes - Print number and exit
No - Print '0'
// 2-digit value - single leading zero already added
Is result >= 10?
Yes - Print number and exit
No - Print '0'
// 1-digit value - two leading zeros already added
Print number
When you multiply the fractional part of the floating point number by 1000 you need to test the result to determine the number of padding zeros to be inserted.
// 3-digit value - print directly
Is result >= 100?
Yes - Print number and exit
No - Print '0'
// 2-digit value - single leading zero already added
Is result >= 10?
Yes - Print number and exit
No - Print '0'
// 1-digit value - two leading zeros already added
Print number