I am trying to do this simple task. Just to format a number using C or C++, but under Windows CE programming.
In this environment, neither inbue nor setlocale methods work.
Finally I did this with no success:
char szValue[10];
sprintf(szValue, "%'8d", iValue);
Any idea?
Why re-invent the wheel and not use functions that are provided for this? See GetNumberFormat.
Custom formatting can be done using the correct NUMBERFMT structure values. For example (pseudo-code):
Finally, I have developed my own function:
I think it's simpler and it does not depend on other classes other than std::string, which I know it will work in Windows Mobile device.
Here's another way, using manual manipulations:
Note: At the time this answer was submitted, the post was tagged C/C++. Now it is tagged C. I suspect it may change again.
Should you want to roll your own C solution which uses C99, the below forms the basis that works on my Windows gcc under various locales.
main
Output
Here's one way - create a custom locale and imbue it with the appropriately customised facet:
expected output:
These functions work in C++, for numbers in string, with or without decimals.
Next function not support negative string numbers or decimal separators, but it was very simple:
Next function support negative string numbers and decimal separators:
And gtest passed tests: