|
|
View previous topic :: View next topic |
Author |
Message |
seidleroni Guest
|
sprintf padding front of string with spaces, what to do? |
Posted: Tue Jul 17, 2007 12:43 pm |
|
|
Hi Everyone,
When I use the following line:
sprintf(s, "%7.3f", fValue);
If the fValue has less than 3 digits in front of the decimal place, then it is padded with spaces. Here is an example:
fValue: New Value:
"1234.123" "1234.123"
"123.123" "123.123"
"12.123" " 12.123"
"1.123" " 1.123"
".123" " .123"
Should I just change the line to be:
sprintf(s, "%0.3f", fValue); ? What is the best way to get rid of the leading spaces?
Thanks |
|
|
Ttelmah Guest
|
|
Posted: Wed Jul 18, 2007 3:24 am |
|
|
Unfortunately, this is going to be difficult:
In standard 'C', this is solved by using a sign character
In C, if the leading character of the field declarator, is _not_ a sign character, then a space _will_ be added to the start of the number output. The only reason it disappears in the top two lines, is that you have run out of width in the output field, so it gets thrown. So far then, CCS is working 'right'.
So, in normal C if you use:
Code: |
sprintf(s, "%+7.3f", fValue);
|
The space should be omitted (since the leading 'sign', gets rid of the space), but the output will now have a '+' sign (not what you want...). However:
Code: |
sprintf(s, "%-7.3f", fValue);
|
Should do what you want. Here the '-' means 'move the value to the left of the field', and since it is a sign character, the leading space should be omitted.
However the problem here is that the sign characters don't work in CCS.
:(
You can 'bodge' round this, by printing the value into a string (as you do), and then using the strchr function to find the last space in the string, and start your output from the character after this.
In the long term, you should 'point this out' to CCS, but they will probably take a while to fix it... :(
Best Wishes |
|
|
Seidleroni Guest
|
|
Posted: Wed Jul 18, 2007 6:24 am |
|
|
In my code, I can use the line
Code: | sprintf(s, "%0.3f", fValue); |
This works well, although I know it is not the proper way it is done. Is there any downside to doing it this way that I may not be aware of? |
|
|
ttelmah Guest
|
|
Posted: Wed Jul 18, 2007 10:10 am |
|
|
No, that is probably perfectly acceptable (though see below for the caveat). Anything up to 4.3, should also work. The standard behaviour, is to print in a field at 'least x wide', where 'x' is the value in front of the DP. If the output is greater than 'x', then the field is automatically expanded.The padding 'space', is only added if the output is less than the specified field width.
I'd be possibly 'chary' of using '0', since this has the special meaning of 'use zero as the padding character', but 4.3, (your minimum width is four characters, with .000), ought to work in any C.
I was assuming you had some need for the field to not drop below 7 characters, so could not use this method.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|