| View previous topic :: View next topic |
| Author |
Message |
Karim Guest
|
Posted: Tue May 24, 2005 9:36 am Post subject: format question |
|
|
Hi,
is there any API to convert a string or an int to a specific format? For example:
my input: 12.3 output: 000012.3
345.9 000345.9
Thanks a lot
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Tue May 24, 2005 11:39 am Post subject: Re: format question |
|
|
"Karim" <Karim (AT) noemail (DOT) com> wrote:
| Quote: |
is there any API to convert a string or an int to a specific format? For example:
my input: 12.3 output: 000012.3
345.9 000345.9
|
That can be accomplished using sprintf or AnsiString.sprintf:
// assumes Buffer is a char*
sprintf( Buffer, "%06f", SomeFloat );
// assumes Buffer is an AnsiString
Buffer.sprintf("%06f", SomeFloat );
~ JD
|
|
| Back to top |
|
 |
Karim Guest
|
Posted: Tue May 24, 2005 2:35 pm Post subject: Re: format question |
|
|
Thanks JD,
What about if I want to change the number after the point in two numbers? 12.3 becomes 000012.30
Thanks
"JD" <nospam (AT) nospam (DOT) com> wrote:
| Quote: |
"Karim" <Karim (AT) noemail (DOT) com> wrote:
is there any API to convert a string or an int to a specific format? For example:
my input: 12.3 output: 000012.3
345.9 000345.9
That can be accomplished using sprintf or AnsiString.sprintf:
// assumes Buffer is a char*
sprintf( Buffer, "%06f", SomeFloat );
// assumes Buffer is an AnsiString
Buffer.sprintf("%06f", SomeFloat );
~ JD
|
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue May 24, 2005 5:27 pm Post subject: Re: format question |
|
|
"Karim" <Karim (AT) noemail (DOT) com> wrote
| Quote: | is there any API to convert a string or an int to a specific format?
|
The Win32 API has a wsprintf() function.
Gambit
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Tue May 24, 2005 10:24 pm Post subject: Re: format question |
|
|
"Karim" <karim (AT) noemail (DOT) com> wrote:
| Quote: |
What about if I want to change the number after the point in
two numbers? 12.3 becomes 000012.30
|
Break the float into 2 ints:
int WholePortion = SomeFloat;
int DecimalPortion = (SomeFloat - WholePortion) * 100;
Buffer.sprintf("%06d.%d", WholePortion, DecimalPortion );
~ JD
|
|
| Back to top |
|
 |
Ed Mulroy [TeamB] Guest
|
Posted: Wed May 25, 2005 1:11 am Post subject: Re: format question |
|
|
| Quote: | What about if I want to change the number after the
point in two numbers? 12.3 becomes 000012.30
|
---------------------
C:Documents and SettingsAdministratorMy DocumentsLookatq099
| Quote: | type test.cpp
#include |
int main()
{
double d = 12.3;
printf("%09.2fn", d);
return 0;
}
C:Documents and SettingsAdministratorMy DocumentsLookatq099
| Quote: | bcc32 -WCR test
Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland |
test.cpp:
Turbo Incremental Link 5.66 Copyright (c) 1997-2002 Borland
C:Documents and SettingsAdministratorMy DocumentsLookatq099
C:Documents and SettingsAdministratorMy DocumentsLookatq099
| Quote: |
--------------------- |
.. Ed
| Quote: | Karim wrote in message
news:42933ba8$1 (AT) newsgroups (DOT) borland.com...
|
|
|
| Back to top |
|
 |
Christian Lotze Guest
|
Posted: Wed May 25, 2005 7:59 am Post subject: Re: format question |
|
|
Remy Lebeau (TeamB) schrieb:
| Quote: | "Karim" <Karim (AT) noemail (DOT) com> wrote in message
news:4292f5a8$1 (AT) newsgroups (DOT) borland.com...
is there any API to convert a string or an int to a specific format?
The Win32 API has a wsprintf() function.
|
wich doesn't work with floating point numbers,
use sprintf, works also in windows.
Or use FmtStr, AnsiString::sprintf or similar formatting functions
Christian
|
|
| Back to top |
|
 |
Heinrich Wolf Guest
|
Posted: Wed May 25, 2005 6:04 pm Post subject: Re: format question |
|
|
"JD" <nospam (AT) nospam (DOT) com> schrieb im Newsbeitrag
news:4293a9ba$1 (AT) newsgroups (DOT) borland.com...
....
| Quote: | int WholePortion = SomeFloat;
int DecimalPortion = (SomeFloat - WholePortion) * 100;
Buffer.sprintf("%06d.%d", WholePortion, DecimalPortion );
|
That will give wrong output on e.g. SomeFloat = 25.06 !
The Format for WholePortion and DecimalPortion must be
"%06d.%02d"
But why for heaven's sake not:
Buffer.sprintf("%06.2f", SomeFloat); ?
|
|
| Back to top |
|
 |
Heinrich Wolf Guest
|
Posted: Wed May 25, 2005 6:06 pm Post subject: Re: format question |
|
|
"Heinrich Wolf" <invalid (AT) invalid (DOT) invalid> schrieb im Newsbeitrag
news:3fjt23F88dlhU1 (AT) individual (DOT) net...
....
| Quote: | Buffer.sprintf("%06.2f", SomeFloat); ?
I'm sorry: "%09.2f" |
|
|
| Back to top |
|
 |
|