Board index » delphi » HOW TO assign float value to numeric field

HOW TO assign float value to numeric field

Hello,
I've an table in an interbase database containing a field named Price of
type numeric(9,2).
my assignment

table.FieldByName('Price').AsFloat = 1.75;

doesn't work (the field contains the value 2 afterwards).

I'm using delphi3.

How can i solve this problem?

Peter Smink
Osirion B.V.
E-mail: psm...@osirion.nl

 

Re:HOW TO assign float value to numeric field


Quote
> I've an table in an interbase database containing a field named Price of
> type numeric(9,2).
> my assignment

> table.FieldByName('Price').AsFloat = 1.75;

> doesn't work (the field contains the value 2 afterwards).

> I'm using delphi3.

> How can i solve this problem?

Apart from using anything other than the BDE to access IB databases... =:-)
IBX, IBOjects and FIB all handle floating point values a lot better than the
BDE.

Firstly, check that the actual value *stored* is 2 and not the value that is
being displayed is 2 and the underlying value is really 1.75. (display based
rounding).

Failing this, have you tried using the .AsCurrency property? Using this can
reduce the rounding problems when passing floating point values across to a
database using the BDE. If the BDE is still being obstinate in that it
continues to round the value, try assigning the field using .AsString - for
some reason this actually cures a lot of BDE based rounding problems. eg.
table.FieldByName( 'Price' ).AsString := Format( '%.2f', [MyFloatVar] ).

Hope this helps

Nick

Re:HOW TO assign float value to numeric field


Thanks for your response,

I've checked your remarks:

1) IBX, IBOjects and FIB all handle floating point values a lot better than
the  BDE.
    I will take a look at this.
2)The value shown in the interbase Database explorer are not rounded. So it
doesn't seem to be a display problem
3) Besides this price format I also have type numeric(6,3) which can not be
presented by a currency type.
4) using table.FieldByName( 'Price' ).AsString := Format( '%.2f',
[MyFloatVar] ) result in an error message that e.g. 1,45 is not a valid
    integer value.

Peter

Re:HOW TO assign float value to numeric field


In addition to Nicks suggestions you might want check the Enable BCD param for
the alias.  Numeric(9,2) will map to a scaled integer (a BCD) and your TField
needs to get mapped to a BCD for that column so that setting should be true.

Quote
Peter Smink wrote:

> Hello,
> I've an table in an interbase database containing a field named Price of
> type numeric(9,2).
> my assignment

> table.FieldByName('Price').AsFloat = 1.75;

> doesn't work (the field contains the value 2 afterwards).

> I'm using delphi3.

> How can i solve this problem?

> Peter Smink
> Osirion B.V.
> E-mail: psm...@osirion.nl

--
Jeff Overcash (TeamB)
      (Please do not email me directly unless  asked. Thank You)
Have you ever met a lady screaming angst potential?
Have you ever dreamed of romance no matter how experimental?
Have you ever felt an alien drifting back into your hometown?
Did you think you were buying safety when you bought that piece of ground?
             (Fish)

Re:HOW TO assign float value to numeric field


Quote
> 4) using table.FieldByName( 'Price' ).AsString := Format( '%.2f',
> [MyFloatVar] ) result in an error message that e.g. 1,45 is not a valid
>     integer value.

In this case you appear to be caught up in regionalisation issues - where
your regional settings use the comma as a decimal separator but the BDE
(maybe IB but probably the BDE) is expecting a dot / decimal point / period
(or whatever you call it - varies) as the decimal separator. If you want to
change this then you can either changed your control panel settings (in
which case other programs may fail) or you could change the decimal
seperator variable  within Delphi.

Nick

Other Threads