4

Hi I want to get numbers from database, for example, if the number in database is 44.7890000000, I would like to get a string 44.789, the same 0.0010000000 -> 0.001, just keep the numbers and trim the tailing '0'. I use this code: qrySth.Fields[i].AsString - it does its job but I find for very small numbers like 0.0000010000 it becomes 1E-6. Is there a way I could disable the scientific notation for this AsString method?

Thanks!

spspli
  • 3,128
  • 11
  • 48
  • 75
  • 3
    I think maybe I need to use displayformat – spspli May 20 '11 at 20:23
  • You should add that comment as an answer, Spspli. – Rob Kennedy May 20 '11 at 20:31
  • Is there a way I use AsString, but can control its display to non-scientific number? – spspli May 20 '11 at 20:35
  • 'DisplayFormat' does not change what 'AsString' returns, it only effects the GetText method of the field which is used in data aware controls. – Sertac Akyuz May 21 '11 at 01:49
  • Related for other languages: [Haskell](http://stackoverflow.com/questions/8098457/how-do-i-get-to-haskell-to-output-numbers-not-in-scientific-notation) [Lua](http://stackoverflow.com/questions/1133639/how-can-i-print-a-huge-number-in-lua-without-using-scientific-notation) [C++ ostreams](http://stackoverflow.com/questions/2335657/prevent-scientific-notation-in-ostream-when-using-with-double) [Delphi](http://stackoverflow.com/questions/6077153/how-to-disable-scientific-notation-in-asstring-in-delphi) – Mechanical snail Aug 22 '12 at 03:40

1 Answers1

2

As an alternative to setting the field's DisplayFormat property, you can read from AsFloat and pass the value directly to FormatFloat. It uses the same format pattern.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • If I use AsFloat, due to the floating number expression nature, it will give me a number not exactly the same as what it is in database. There will be extra rounding problem. So I don't want to use AsFloat – spspli May 20 '11 at 20:34
  • 1
    What type is the database field? (I don't mean the Delphi TField class. I mean the field in the DB schema.) – Rob Kennedy May 20 '11 at 20:42
  • 1
    @spspli - Both 'AsFloat' and 'AsString' will first retrieve the field's value from the database, both will use the `GetData` method. You have a problem with one, you have it with both. 'AsString' will, *additionally*, convert the value to string using [`FloatToStr`](http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/SysUtils_FloatToStr@Extended.html), which uses 'ffGeneral' for [FloatFormat](http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/SysUtils_TFloatFormat.html) with 15 significant digits. – Sertac Akyuz May 21 '11 at 01:47