Recent

Author Topic: using AsInteger in a Float Field  (Read 3596 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
using AsInteger in a Float Field
« on: July 21, 2021, 11:17:45 am »
Hello :)

so i was wondering what happens if u take value of Float Field in Database by using Fieldbyname.AsInteger. Does AsInteger Round ? or trunc ? does an error occur ?
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: using AsInteger in a Float Field
« Reply #1 on: July 21, 2021, 11:29:50 am »
As_xxx are TField's methods that transtype internally. In TField.As_xxx, xxx means, indicates the type of data we provide (when it is an assignment, i.e. aField.As_xxx:= x;). So, the field knows, then, if it should transtype x before its effective storage (in accordance with its true type).

And for the opposite, it's simply asking the TField to transtype its internal type towards an external typed variable. So yes: when asking for the value with AsInteger (your question), the TFloatField should round its native real value towards the nearest integer.
« Last Edit: July 21, 2021, 11:49:45 am by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Re: using AsInteger in a Float Field
« Reply #2 on: July 21, 2021, 03:08:18 pm »
Thna
As_xxx are TField's methods that transtype internally. In TField.As_xxx, xxx means, indicates the type of data we provide (when it is an assignment, i.e. aField.As_xxx:= x;). So, the field knows, then, if it should transtype x before its effective storage (in accordance with its true type).

And for the opposite, it's simply asking the TField to transtype its internal type towards an external typed variable. So yes: when asking for the value with AsInteger (your question), the TFloatField should round its native real value towards the nearest integer.


Very Helpful thank you :)

So i could use ?: 
FieldByName('IntegerField').AsFloat --> Return : Float (BSp: 2.0)
FieldByName('IntegerField').AsString--> Return : Float (BSp: '2')
FieldByName('IntegerField').AsInteger--> Return : Float (BSp: 2)

or

FieldByName('IntegerField').asInteger := IntValue --> In DB Filed would be: 2
FieldByName('IntegerField').AsString := StrValue --> In DB Filed would be: 2
FieldByName('IntegerField').AsFloat := FloatValue --> In DB Filed would be: 2
« Last Edit: July 21, 2021, 03:18:05 pm by Weitentaaal »
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: using AsInteger in a Float Field
« Reply #3 on: July 21, 2021, 04:31:37 pm »
So i could use ?: [snip] 

AFAIK, yes: that's what the TField's As_xxx method are for.

use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Re: using AsInteger in a Float Field
« Reply #4 on: July 22, 2021, 04:12:59 pm »
Much Thanks  ;)
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

 

TinyPortal © 2005-2018