# Floating point number to 1 decimal place

#### 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

Oh, I am using XC8 compiler for a PIC

#### MrChips

Joined Oct 2, 2009
22,525
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;

#### 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
22,525
Try

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

#### MrChips

Joined Oct 2, 2009
22,525

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

Rich (BB code):
str[3]= 0x2E;

#### 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
22,525
After you have packed the single characters into a string you now have a single string.
Call displaySmallString( ) once.

#### djsfantasi

Joined Apr 11, 2010
7,225
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.

#### 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.

#### djsfantasi

Joined Apr 11, 2010
7,225
Add one more array element and set it to 0 (not\0)
Rich (BB code):
str[5]=0;

#### 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.

#### 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
22,525
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.

#### MrChips

Joined Oct 2, 2009
22,525
And another thing, '0' is not the same as "0".

'0' is a character.

"0" is a string.

#### THE_RB

Joined Feb 11, 2008
5,438

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.

#### 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.

#### MrChips

Joined Oct 2, 2009
22,525
Try

char buf[6];

#### MrChips

Joined Oct 2, 2009
22,525
No one needs to store zero in a character string.

The ASCII character for the numeral 0 is 48 or 0x30.

#### ErnieM

Joined Apr 24, 2011
8,082
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.