Board index » cppbuilder » StrToFloat

StrToFloat


2005-09-20 06:05:52 PM
cppbuilder113
Ho can i convert a string representing a currency value (es: "? 30,50") in a
double value? I tried the function "StrToFloat" but without result!!!
Thanks...
--
Chri.
 
 

Re:StrToFloat

Chri wrote:
Quote
Ho can i convert a string representing a currency value (es: "?
30,50") in a double value? I tried the function "StrToFloat" but
without result!!!
Firstly you probably shouldn't store currency in a floating point
variable. Floating point types can introduce innacuracies during
calculations due to the unavoidable approximations of digital
processing. It's better to store the values as integers and factor
up/down as and when needed.
There is also a Currency type in BCB6.
Secondly you say 'without result'. Presumably something must have
happened when you called the function. If you could provide us with a
little more detail as to what actually happened we might be able to
provide a better solution.
One thing does occur to me though:Your floating point number has a
comma as the decimal separator. Is this how your system normally
displays floating point numbers? If not (ie;if your system is
configured for the UK or US) then the conversion will fail since
'30,50' is not a valid floating point number on those systems and you
should get an exception saying so.
--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html
 

Re:StrToFloat

On Tue, 20 Sep 2005 13:19:37 +0300, Andrue Cope [TeamB]
< XXXX@XXXXX.COM >wrote:
Quote
Chri wrote:

>Ho can i convert a string representing a currency value (es: "?
>30,50") in a double value? I tried the function "StrToFloat" but
>without result!!!

Firstly you probably shouldn't store currency in a floating point
variable. Floating point types can introduce innacuracies during
calculations due to the unavoidable approximations of digital
processing. It's better to store the values as integers and factor
up/down as and when needed.

There is also a Currency type in BCB6.

Secondly you say 'without result'. Presumably something must have
happened when you called the function. If you could provide us with a
little more detail as to what actually happened we might be able to
provide a better solution.

One thing does occur to me though:Your floating point number has a
comma as the decimal separator. Is this how your system normally
displays floating point numbers? If not (ie;if your system is
configured for the UK or US) then the conversion will fail since
'30,50' is not a valid floating point number on those systems and you
should get an exception saying so.

Also, IIRC, functions like StrToFloat or atof stop when they encounter
unreckognisable characters in the string. My current trick when I do stuff
like that is something like: float value=atof(&string[x]); where x=(the
number of characters to skip within the string in order to get to the
value)
--
Using Opera's revolutionary e-mail client: www.opera.com/mail/
 

{smallsort}

Re:StrToFloat

"George Nakos" < XXXX@XXXXX.COM >wrote in message
On Tue, 20 Sep 2005 13:19:37 +0300, Andrue Cope [TeamB]
< XXXX@XXXXX.COM >wrote:
Also, IIRC, functions like StrToFloat or atof stop when they
encounter
unreckognisable characters in the string.
That's only true for atof. StrToFloat will throw an exception.
- Arnie
 

Re:StrToFloat

On Wed, 21 Sep 2005 00:18:20 +0300, Arnie <none>wrote:
Quote
That's only true for atof. StrToFloat will throw an exception.

- Arnie

Ah, yes, I remember stumbling upon this one. That's why i prefer atof and
atoi
--
Using Opera's revolutionary e-mail client: www.opera.com/mail/