How to disable scientific notation in .AsString in Delphi?
Asked Answered
C

1

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!

Cologne answered 20/5, 2011 at 20:16 Comment(5)
I think maybe I need to use displayformatCologne
You should add that comment as an answer, Spspli.Antedate
Is there a way I use AsString, but can control its display to non-scientific number?Cologne
'DisplayFormat' does not change what 'AsString' returns, it only effects the GetText method of the field which is used in data aware controls.Fulk
Related for other languages: Haskell Lua C++ ostreams DelphiPulley
A
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.

Antedate answered 20/5, 2011 at 20:31 Comment(3)
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 AsFloatCologne
What type is the database field? (I don't mean the Delphi TField class. I mean the field in the DB schema.)Antedate
@Cologne - 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, which uses 'ffGeneral' for FloatFormat with 15 significant digits.Fulk

© 2022 - 2024 — McMap. All rights reserved.