Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

How can I convert a float value into a String object without implementing any library?

I'd like to avoid using char array.

share|improve this question
1  
Why would you want to "avoid" a char array? Surely the String is the one to avoid? –  Majenko Jul 11 at 21:09
    
Why is char better than string? –  Federico Corazza Jul 11 at 21:12
2  
Because Strings use dynamic allocation which is bad on a microcontroller with only 2K or RAM. With char you usually use static allocation and just allocate what you need. Strings are evil - a crutch for people who can't be bothered to do it properly. –  Majenko Jul 11 at 21:15
    
I've been trying to convert integers, floats, dates and strings and String looked to be the easiest. So how can I convert all that in a char? –  Federico Corazza Jul 11 at 21:19
2  
No. A "char array" is the native "string" format in C. The String object is an Arduino specific wrapper around it that adds extra bloat. –  Majenko Jul 11 at 21:24

1 Answer 1

up vote 2 down vote accepted

If you want to do it manually, rather than using the standard tools (dtostrf() for example) then you need to think mathematically.

A floating point number is made up of two distinct parts - the integer part and the decimal part.

For instance the number 123.4567 is made up of the integer part 123 and the decimal part 4567. Chopping off the integer portion is simple to do, and if that happens to be just one digit then you can simply add that as the first digit of your output. In this case, though, it isn't.

So what to do? Simple: keep dividing the number by 10 until it is just one digit. But be sure to count the number of times you divide by 10. So the example above would end up as 1.234567 with a divide count of 2. This is the same as the scientific representation, 1.234567×10².

So now you have it divided you can chop off the integer (just cast it to an int), add that integer part to your string, then remove it from the source number. So you end up with "1" and 0.234567 and a count of 2.

Now multiply by 10 and subtract one from your count, so you get 2.34567 and a count of 1. Grab the integer portion and add it to your string, then remove it from your number.

Keep repeating until your count is 0, at which point you have now done all the integer portion - so you can now add a "." to your string.

You now just keep repeating the same process again and again until the absolute value of the count equals the number of decimal places you want to print.

For negative numbers you should start by placing a "-" in your string, then taking the absolute value of the number (the number as a positive).

Or you just use the standard library function dtostrf().

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.