Board index » cppbuilder » format question

format question


2005-05-24 05:36:40 PM
cppbuilder69
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
 
 

Re:format question

"Karim" < XXXX@XXXXX.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
 

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" < XXXX@XXXXX.COM >wrote:
Quote

"Karim" < XXXX@XXXXX.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

 

{smallsort}

Re:format question

"Karim" < XXXX@XXXXX.COM >wrote in message
Quote
is there any API to convert a string or an int to a specific format?
The Win32 API has a wsprintf() function.
Gambit
 

Re:format question

"Karim" < XXXX@XXXXX.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
 

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 Settings\Administrator\My Documents\Lookat\q099
Quote
type test.cpp
#include <stdio.h>
int main()
{
double d = 12.3;
printf("%09.2f\n", d);
return 0;
}
C:\Documents and Settings\Administrator\My Documents\Lookat\q099
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 Settings\Administrator\My Documents\Lookat\q099
Quote
test
000012.30
C:\Documents and Settings\Administrator\My Documents\Lookat\q099
Quote

---------------------
. Ed
Quote
Karim wrote in message
news:42933ba8$ XXXX@XXXXX.COM ...
 

Re:format question

Remy Lebeau (TeamB) schrieb:
Quote
"Karim" < XXXX@XXXXX.COM >wrote in message
news:4292f5a8$ XXXX@XXXXX.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
Quote

Gambit

Christian
 

Re:format question

"JD" < XXXX@XXXXX.COM >schrieb im Newsbeitrag
...
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); ?
 

Re:format question

"Heinrich Wolf" < XXXX@XXXXX.COM >schrieb im Newsbeitrag
...
Quote
Buffer.sprintf("%06.2f", SomeFloat); ?
I'm sorry: "%09.2f"