Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Remove exponent from formatted float in Delphi

Tags:

delphi

Given a double value like 1.00500000274996E-8, how do I convert it to it's non scientific format with a maximum number of digits after the decimal point - in this case with 8 digits it would be 1.00500000?

The conversion should not pad with zeros, so 2007 would come out as 2007, and 2012.33 and 2012.33.

I've tried lots of combinations using Format, FormatFloat, FloatToStrF but can't quite seem to hit the jackpot. Many thanks for any help.

Edit: I should clarify that I am trying to convert it to a string representation, without the Exponent (E) part.

like image 391
Alan Clark Avatar asked Jul 25 '11 21:07

Alan Clark


2 Answers

FormatFloat('0.######################', 1.00500000274996E-8) should do the trick.

Output is: 0,0000000100500000274996

It will not output more digits than absolutely necessary.

like image 167
JRL Avatar answered Sep 20 '22 02:09

JRL


See John Herbster's Exact Float to String Routines in CodeCentral. Perhaps not exactly what youre after but might be good starting point... CC item's description:

This module includes
(a) functions for converting a floating binary point number to its *exact* decimal representation in an AnsiString;
(b) functions for parsing the floating point types into sign, exponent, and mantissa; and
(c) function for analyzing a extended float number into its type (zero, normal, infinity, etc.)

Its intended use is for trouble shooting problems with floating point numbers.

His DecimalRounding routines might be of intrest too.

like image 23
ain Avatar answered Sep 22 '22 02:09

ain