Floating point number to 1 decimal place

Thread Starter

portreathbeach

Joined Mar 7, 2010
143
I have a floating point number and want to round it to 1 decimal place and also want to display the .0 if it ends up a whole number. I then want to make this into a string to use in another function to display it on my LCD.

So far I have this:

Rich (BB code):
    uint16 wholeNumber = trunc(val);                        // truncate the value to get the whole number

    uint16 afterDP = round((val*10) - (wholeNumber*10));    // get 1 DP by rounding (val*10 - wholeNumber*10)

    uint8 char1 = wholeNumber/100;

    uint8 char2 = (wholeNumber - (char1*100))/10;

    uint8 char3 = wholeNumber-(char1*100)-(char2*10);
This gives me the hundreds, tens and units and the decimal place numbers into char1, char2, char3 and afterDP.

To get the ASCII values, I will add 0x30 to each character.

I already have a function I can call to display a string to my screen, the function is:

Rich (BB code):
void displaySmallString(uint8 x, uint8 y, char *characters);
I can call it by:

Rich (BB code):
displaySmallString(10,10, "Hello");
But what I want to do is be able to call the function with a string that is made up of the characters; char1, char2, char3, a decimal point, afterDP

How do I go about this?



Oh, I am using XC8 compiler for a PIC
 

MrChips

Joined Oct 2, 2009
30,714
You can pack the ASCII characters into the string one at a time:

Rich (BB code):
char s[10];

s[0] = char1;
s[1] = char2;
s[2] = char3;
s[3] = '.';
s[4] = afterDP;
s[5] = 0;
 

Thread Starter

portreathbeach

Joined Mar 7, 2010
143
Hi,

That is pretty much what I have also:

char str[5];

Rich (BB code):
    str[0]= char1; + 0x30;
    str[1]= char2; + 0x30;
    str[2]= char3; + 0x30;
    str[3]= (".");
    str[4]= afterDP + 0x30;
I'm adding 0x30 to make it the equivalent ASCII character code.

The problem is, how do I call my function to display this, it will not accept:

Rich (BB code):
 while (*str){
    displaySmallChar(10, 10, *str++);
}
 

MrChips

Joined Oct 2, 2009
30,714
Try


Rich (BB code):
displaySmallString(10,10, &str[0]);
BTW, what we are discussing is called fixed point arithmetic, not floating point arithmetic.
 

Thread Starter

portreathbeach

Joined Mar 7, 2010
143
Yep, that's done it.

But is there not a way to put the characters into a kind of string? Like, when I call the function with "Hello". Instead of having to call the function 5 times
 

MrChips

Joined Oct 2, 2009
30,714
After you have packed the single characters into a string you now have a single string.
Call displaySmallString( ) once.
 

djsfantasi

Joined Apr 11, 2010
9,156
Small point, but in your code you have a typo. It should look like:
Rich (BB code):
    str[0]= char1 + 0x30;
    str[1]= char2 + 0x30;
    str[2]= char3 + 0x30;
    str[3]= (".");
    str[4]= afterDP + 0x30;
in the first three lines, the first semi-colon is incorrect. Remove it.
 

Thread Starter

portreathbeach

Joined Mar 7, 2010
143
OK. All working now apart from when I pass the string into the function, I have a

while (*character++){
.....calls a function to do something
}

The problem is that the array needs a null on the end I think but I can't get it to work. I tried putting a '\0' on the end but it didn't work.
 

Thread Starter

portreathbeach

Joined Mar 7, 2010
143
That doesn't work either.

As some of the characters passed in could actually be a 0. How does it determine the null 0 at the end from an actual 0 that you may want.

The while(*chacters++){
}

keeps going past the end of the array and then gets random characters and the program jumps all over the place.
 

Thread Starter

portreathbeach

Joined Mar 7, 2010
143
If I send "hello" to the lcd display function, the

while (*characters){

}

stops after the last character, but when sending it my array I made it doesn't know where the end of it is.

So when sending "hello" what is telling it what is the end?
 

MrChips

Joined Oct 2, 2009
30,714
No, No, No.

You have to learn how characters are packed in a string. This is the correct code.

Rich (BB code):
str[5] = 0;
If this does not work then you are doing something else incorrectly.

Avoid trying to work around a problem without understanding the problem. Do it right in the first place.

The C compiler puts a NUL character at the end of the string "hello".

What is the ASCII for NUL character? It is zero, not the numeral zero.
 

THE_RB

Joined Feb 11, 2008
5,438
Instead of:

Rich (BB code):
str[3]= (".");
use:

Rich (BB code):
str[3]= 0x2E;
I use this;
Rich (BB code):
str[3]= ('.');
Which gives the the benefit of ASCII char code readability, but does not carry the trailing zero caused by "" ASCII string tags.
 

Thread Starter

portreathbeach

Joined Mar 7, 2010
143
OK.

Please can someone tell me why the first time callStringToSend is called, it works, but when I call it with the contents of 'buf', it keeps going past the end of the string.

Rich (BB code):
void display(char *character){
.... the code to display something on my screen
}


void takeStringToSend(char *ch){
   while (*ch){
   display (*ch++)
}}
}



void main{

char buf[5];
buf[0] = 0x31;     //ASCII 1
buf[1] = 0x32;;     //ASCII 2
buf[2] = 0x33;;     //ASCII 3
buf[3] = 0x2E;;     //ASCII .
buf[4] = 0x34;;     //ASCII 4
buf[5] = 0;


takeStringToSend("123.4");

takeStringToSend(buf);

}
Also, I still don't understand the null value at the end. What if you had an array where you wanted to store a 0 in it? How does it differentiate between a null and a 0.
 

ErnieM

Joined Apr 24, 2011
8,377
I use this;
Rich (BB code):
str[3]= ('.');
Which gives the the benefit of ASCII char code readability, but does not carry the trailing zero caused by "" ASCII string tags.
QFT.

Note the single quote is used for a single character, while a double quote is used for a zero terminated string.

This is the preferred form as there are no magic numbers here (that are obscure and not obviously correct or incorrect).

A child could tell you what the character is.
 

Thread Starter

portreathbeach

Joined Mar 7, 2010
143
I'll have a go when I get home.

What would happen if you actually wanted an array of numbers. If one of the numbers stored in the array way 0, would the C routine that has the: while(*characters) nit think that it was the end of the array when it got to a number 0 which you may actually want stored in the array.
 
Top