-
String issue
Hi I am having a problem with inserting a decimal point into a string.
Okay here it goes,
I am writing to an LCD display and I am having trouble converting a number to be displayed. For example, I have an integer that ranges from 1 to 9999 I need to display this integer in string format with a decimal point before the last character so 1 would be ' '' '' ''.''1' and 9999 would be '9''9''9''.''9' . Currently I have code that takes the number to string format directly but how do I add the decimal point and spaces to the left of the string to make sure it is printed correctly?
Thanks
Chris
-
If you have four characters, and you want to turn it into five, you could do something like this:
[1] -> [1]
[2] -> [2]
[3] -> [3]
"." -> [4]
[4] -> [5]
But maybe I'm not understanding the question.
-
If you already have the number in a std::string, then try something like this:
Code:
void addDec(std::string &s)
{
s.insert(s.length()-1, 1, '.');
}
If you're using a char * instead of std::string, the code would be somewhat similar.
Edit: Fixed idiocy in code.