BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

FormatFLoat

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Non-Technical)
View previous topic :: View next topic  
Author Message
Mike Margerum
Guest





PostPosted: Mon Mar 14, 2005 5:43 pm    Post subject: FormatFLoat Reply with quote



float f=3.24;
AnsiString str;
str.FormatFloat("$##.00",f);
ShowMessage(str);
str = FormatFloat("$##.00",f);
ShowMessage(str);

The first ShowMessage is blank. The 2nd one works. why?
Back to top
Vladimir Stefanovic
Guest





PostPosted: Mon Mar 14, 2005 5:54 pm    Post subject: Re: FormatFLoat Reply with quote



Not sure, but you need this:
str = str.FormatFloat( "$##.00", f );

--
Best regards,
Vladimir Stefanovic
"Mike Margerum" <mike (AT) garbage (DOT) com> wrote

Quote:
float f=3.24;
AnsiString str;
str.FormatFloat("$##.00",f);
ShowMessage(str);
str = FormatFloat("$##.00",f);
ShowMessage(str);

The first ShowMessage is blank. The 2nd one works. why?



Back to top
Micha Nelissen
Guest





PostPosted: Mon Mar 14, 2005 6:48 pm    Post subject: Re: FormatFLoat Reply with quote



On Mon, 14 Mar 2005 12:43:11 -0500
Mike Margerum <mike (AT) garbage (DOT) com> wrote:

Quote:
float f=3.24;
AnsiString str;
str.FormatFloat("$##.00",f);
ShowMessage(str);
str = FormatFloat("$##.00",f);
ShowMessage(str);

The first ShowMessage is blank. The 2nd one works. why?

I vaguely recall FormatFloat being a class method of ansistrings. It returns the formatted value. (AnsiString.FormatFloat(...) would be clearer, but that is delphi -> c++ heritage?)

Micha

Back to top
mr_organic
Guest





PostPosted: Mon Mar 14, 2005 6:54 pm    Post subject: Re: FormatFLoat Reply with quote

"Vladimir Stefanovic" <antivari (AT) po (DOT) sbb.co.yu> wrote in
news:4235cfdf (AT) newsgroups (DOT) borland.com:

Quote:
Not sure, but you need this:
str = str.FormatFloat( "$##.00", f );


I think it's because the FormatFloat method of the AnsiString is a static
method, not an instance method, and returns a new instance of AnsiString.
FormatFloat (the global method) does the same, but is not invoked as a
static member.

mr_organic

Back to top
Michael Gillen
Guest





PostPosted: Mon Mar 14, 2005 6:59 pm    Post subject: Re: FormatFLoat Reply with quote

Mike Margerum wrote:

Quote:
float f=3.24;
AnsiString str;
str.FormatFloat("$##.00",f);
ShowMessage(str);
str = FormatFloat("$##.00",f);
ShowMessage(str);

The first ShowMessage is blank. The 2nd one works. why?

In the first case, you have an emtry string, then told it to format a float. However, you never
told it to store it anywhere. It only used the objects ability to format the float.
Try ShowMessage(str.FormatFloat("$##.00",f)), or better yet ShowMessage(FormatFloat("$##.00",f));

--
-Michael Gillen

Back to top
Mike Margerum
Guest





PostPosted: Mon Mar 14, 2005 8:16 pm    Post subject: Re: FormatFLoat Reply with quote

AHHHH i c. I just assumed it would operate on the object invoking it. lol

Thx

mr_organic wrote:
Quote:
"Vladimir Stefanovic" <antivari (AT) po (DOT) sbb.co.yu> wrote in
news:4235cfdf (AT) newsgroups (DOT) borland.com:


Not sure, but you need this:
str = str.FormatFloat( "$##.00", f );



I think it's because the FormatFloat method of the AnsiString is a static
method, not an instance method, and returns a new instance of AnsiString.
FormatFloat (the global method) does the same, but is not invoked as a
static member.

mr_organic

Back to top
Mike Margerum
Guest





PostPosted: Tue Mar 15, 2005 3:09 am    Post subject: Re: FormatFLoat Reply with quote

The thing that made me assume it wasnt static was because the sprintf
method isnt static. Strange.

mr_organic wrote:
Quote:
"Vladimir Stefanovic" <antivari (AT) po (DOT) sbb.co.yu> wrote in
news:4235cfdf (AT) newsgroups (DOT) borland.com:


Not sure, but you need this:
str = str.FormatFloat( "$##.00", f );



I think it's because the FormatFloat method of the AnsiString is a static
method, not an instance method, and returns a new instance of AnsiString.
FormatFloat (the global method) does the same, but is not invoked as a
static member.

mr_organic

Back to top
mr_organic
Guest





PostPosted: Wed Mar 16, 2005 1:28 pm    Post subject: Re: FormatFLoat Reply with quote

Mike Margerum <mike (AT) garbage (DOT) com> wrote in
news:42365202$1 (AT) newsgroups (DOT) borland.com:

Quote:
The thing that made me assume it wasnt static was because the sprintf
method isnt static. Strange.


I've noticed that many static methods of classes are aliased as globals:
Trim(), IntToStr(), FormatDateTime(), and so on. It might just be a
convenience thing, but I'm not sure. For consistency, and to make the
conversion clear, I generally use the static method of the class and not
the global method, like so:

I write

AnsiString strFoo = strTemp.Trim().LowerCase();

instead of

AnsiString strFoo = Trim(strTemp);
strFoo = LowerCase(strFoo);

It condenses the code without compromising clarity.

mr_organic


Back to top
Micha Nelissen
Guest





PostPosted: Wed Mar 16, 2005 3:14 pm    Post subject: Re: FormatFLoat Reply with quote

On 16 Mar 2005 05:28:35 -0800
"mr_organic" <mr_organic (AT) yourmamashouse (DOT) com> wrote:

Quote:
AnsiString strFoo = Trim(strTemp);
strFoo = LowerCase(strFoo);

It condenses the code without compromising clarity.

Why not:

AnsiString strFoo = LowerCase(Trim(strTemp));

?

Micha

Back to top
mr_organic
Guest





PostPosted: Wed Mar 16, 2005 3:29 pm    Post subject: Re: FormatFLoat Reply with quote

It condenses the code without compromising clarity.
Quote:

Why not:

AnsiString strFoo = LowerCase(Trim(strTemp));

?

Micha


*Shrug*. No reason. Just a style thing, I guess. One's as good as the
other.

mr_organic

Back to top
Duane Hebert
Guest





PostPosted: Wed Mar 16, 2005 4:04 pm    Post subject: Re: FormatFLoat Reply with quote


"mr_organic" <mr_organic (AT) yourmamashouse (DOT) com> wrote

Quote:
It condenses the code without compromising clarity.

Why not:

AnsiString strFoo = LowerCase(Trim(strTemp));

?

Micha


*Shrug*. No reason. Just a style thing, I guess. One's as good as the
other.

I would prefer if static functions were called with the namespace/class name
to show that they're static. It's probably not a popular opinion but
I would prefer:

AnsiString strFoo = AnsiString::LowerCase(AnsiString::Trim(strTemp));

This indicates that it's a static function and in the AnsiString namespace.


Back to top
Tamas Demjen
Guest





PostPosted: Fri Mar 18, 2005 10:35 pm    Post subject: Re: FormatFLoat Reply with quote

That wouldn't work, LowerCase and Trim are non-static const member
functions. You either

output = input.Trim();

or

output = Trim(input);

I agree, there's a little bit of inconsistency between the sprintf and
FormatFloat members. Also, AnsiString has a .ToIntDef() member, as well
as a ToDouble(), but not a .ToDoubleDef(), which would be convenient
sometimes.

My biggest problem with string classes is that WideString's data access
functions are buggy. Like

BSTR __fastcall c_bstr() const { return Data; }

should be

BSTR __fastcall c_bstr() const { return Data ? Data : L""; }

An empty string shouldn't be NULL but "" instead. It can cause a lot of
crashes and headache. For example, this doesn't work:
WideString wide_string;
std::wstring s = wide_string.c_bstr(); // boom!

Hope this will be fixed in the new BCB. AnsiString doesn't have this bug.

Tom


Quote:
I would prefer if static functions were called with the namespace/class name
to show that they're static. It's probably not a popular opinion but
I would prefer:

AnsiString strFoo = AnsiString::LowerCase(AnsiString::Trim(strTemp));

This indicates that it's a static function and in the AnsiString namespace.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Non-Technical) All times are GMT
Page 1 of 1

 
Jump to:  
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, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.