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 

EditBox and Float

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





PostPosted: Mon Jul 17, 2006 10:37 pm    Post subject: EditBox and Float Reply with quote



My code:

void __fastcall TNovaMistura::Button3Click(TObject *Sender)
{
float a11, a12, total;
a11 = StrToFloat(Edit11->Text);
a12 = StrToFloat(Edit12->Text);
total = StrToFloat(Edit31->Text);
a11 = a11/100;
a12 = a12/100;
Edit21->Text = FloatToStr(a11*total);
Edit22->Text = FloatToStr(a12*total);
}
}
Edit21 always gets numbers like these: 16,555666
I want to set it to only 2 units after coma : 16,56 (I don't know if your gonna understand what I have sad, but if don't, I will try again.)
And also if you have suggestions about how to optimize the code.

Renan
Back to top
Darío Alejandro Guzik
Guest





PostPosted: Tue Jul 18, 2006 12:26 am    Post subject: Re: EditBox and Float Reply with quote



Hi!

When working with floating point numbers you have to be carefull because
not all real numbers are representable with floating point numbers. In
those cases the number stored in the float variable will be the floating
point number that is the closest to the number you wanted to store.
If you want to show the number rounded to 2 decimals you could use
FloatToStrF, but have in mind that the number you show is not the number
you have stored.

If you want to work with numbers having only 2 decimal places, I would
recommend NOT using float or doubles.

Renan wrote:
Quote:
My code:

void __fastcall TNovaMistura::Button3Click(TObject *Sender)
{
float a11, a12, total;
a11 = StrToFloat(Edit11->Text);
a12 = StrToFloat(Edit12->Text);
total = StrToFloat(Edit31->Text);
a11 = a11/100;
a12 = a12/100;
Edit21->Text = FloatToStr(a11*total);
Edit22->Text = FloatToStr(a12*total);
}
}
Edit21 always gets numbers like these: 16,555666
I want to set it to only 2 units after coma : 16,56 (I don't know if your gonna understand what I have sad, but if don't, I will try again.)
And also if you have suggestions about how to optimize the code.

Renan

--
Darío Alejandro Guzik (El Tío Borracho)
http://tioborracho.tripod.com
e-mail: daguzik (AT) dc (DOT) uba.ar
ICQ : 8389493
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Tue Jul 18, 2006 12:27 am    Post subject: Re: EditBox and Float Reply with quote



"Renan" <gulbrrs (AT) gmail (DOT) com> wrote in message
news:44bbcae0$1 (AT) newsgroups (DOT) borland.com...

Quote:
Edit21 always gets numbers like these: 16,555666
I want to set it to only 2 units after coma : 16,56

Look at FormatFloat()


Gambit
Back to top
MrT
Guest





PostPosted: Tue Jul 18, 2006 6:31 am    Post subject: Re: EditBox and Float Reply with quote

Darío Alejandro Guzik wrote:

Quote:
Hi!

When working with floating point numbers you have to be carefull because
not all real numbers are representable with floating point numbers. In
those cases the number stored in the float variable will be the floating
point number that is the closest to the number you wanted to store.
If you want to show the number rounded to 2 decimals you could use
FloatToStrF, but have in mind that the number you show is not the number
you have stored.

If you want to work with numbers having only 2 decimal places, I would
recommend NOT using float or doubles.


And what would you suggest?

Quote:
Renan wrote:

My code:

void __fastcall TNovaMistura::Button3Click(TObject *Sender)
{
float a11, a12, total;
a11 = StrToFloat(Edit11->Text);
a12 = StrToFloat(Edit12->Text);
total = StrToFloat(Edit31->Text);
a11 = a11/100;
a12 = a12/100;
Edit21->Text = FloatToStr(a11*total);
Edit22->Text = FloatToStr(a12*total);
}
}
Edit21 always gets numbers like these: 16,555666
I want to set it to only 2 units after coma : 16,56 (I don't know if
your gonna understand what I have sad, but if don't, I will try again.)
And also if you have suggestions about how to optimize the code.

Renan

Back to top
Thorsten Kettner
Guest





PostPosted: Tue Jul 18, 2006 1:29 pm    Post subject: Re: EditBox and Float Reply with quote

"Renan" <gulbrrs (AT) gmail (DOT) com> wrote:
Quote:
float a11, a12, total;
a11 = StrToFloat(Edit11->Text);
a12 = StrToFloat(Edit12->Text);
total = StrToFloat(Edit31->Text);
a11 = a11/100;
a12 = a12/100;
Edit21->Text = FloatToStr(a11*total);
Edit22->Text = FloatToStr(a12*total);

Floating point variables are not precise, but only approximating
the actual value.

If you want to use floating point types though, then use double
instead of float. It uses some bytes more for storing the value
and is thus more precise. (There is even the type long double
available, but not all available functions support that type).

When displaying floating point values, always format the output
(with FormatFloat for instance).

If you stay in the range of up two four decimal places and want
more accuracy then you could use Currency as a fixed point type:

Currency a11, a12, total;
a11 = StrToCurr(Edit11->Text);

If you even need more precision then look out for a so-called
rational library.
Back to top
Darío Alejandro Guzik
Guest





PostPosted: Tue Jul 18, 2006 7:30 pm    Post subject: Re: EditBox and Float Reply with quote

MrT wrote:
Quote:
Darío Alejandro Guzik wrote:

Hi!

When working with floating point numbers you have to be carefull
because not all real numbers are representable with floating point
numbers. In those cases the number stored in the float variable will
be the floating point number that is the closest to the number you
wanted to store.
If you want to show the number rounded to 2 decimals you could use
FloatToStrF, but have in mind that the number you show is not the
number you have stored.

If you want to work with numbers having only 2 decimal places, I would
recommend NOT using float or doubles.


And what would you suggest?

It deppends on what are you doing. Another user recommended Currency, I

would recommend just using ints. It all deppends on your needs. I didn't
lok for it but there must be dozens of fixed point numbers classes for
c++ around.
--
Darío Alejandro Guzik (El Tío Borracho)
http://tioborracho.tripod.com
e-mail: daguzik (AT) dc (DOT) uba.ar
ICQ : 8389493
Back to top
Thorsten Kettner
Guest





PostPosted: Wed Jul 19, 2006 7:30 pm    Post subject: Re: EditBox and Float Reply with quote

"Renan" <gulbrrs (AT) gmail (DOT) com> wrote:
Quote:
I did use FloatToStrF, It works for what I'm trying to do.

float a11, a12, total;
a11 = StrToFloat(Edit11->Text);
a12 = StrToFloat(Edit12->Text);
total = StrToFloat(Edit31->Text);
a11 = a11/100;
a12 = a12/100;
Edit21->Text = FloatToStrF(a11*total,ffFixed,6,2);
Edit22->Text = FloatToStrF(a11*total,ffFixed,6,2);

Okay. As you mention in another thread that you are a beginner,
please notice: the word "Float" in StrToFloat and FloatToStrF
mean "floating point variable", not necessarily a float. It is
even so that the type float is rarely the appropriate type.
As mentioned, floating point vars are inaccurate. There are
three types available:

float (4 bytes) precison = 7 digits
double (8 bytes) precision = 15 digits
long double (80 bits) precision = 18 digits

As the values are stored approximated, would you be ready to
lose precision, just to save 4 bytes? I wouldn't usually. There
are functiuons that don't support long double, so I usually
use double, but never float.
Back to top
MrT
Guest





PostPosted: Mon Aug 21, 2006 6:30 am    Post subject: Re: EditBox and Float Reply with quote

Hi:

Why is mynposting in this thread:

Error!
newsgroup server responded:no such article number in this group

Perhaps the article has expired

<44bc39a1$1 (AT) newsgroups (DOT) borland.com> (46102)

Remy Lebeau (TeamB) wrote:

Quote:
"Renan" <gulbrrs (AT) gmail (DOT) com> wrote in message
news:44bbcae0$1 (AT) newsgroups (DOT) borland.com...


Edit21 always gets numbers like these: 16,555666
I want to set it to only 2 units after coma : 16,56


Look at FormatFloat()


Gambit

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) 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.