[Lazarus] formatfloat negative zero (-0)

Andrea Mauri andrea.mauri.75 at gmail.com
Wed Jul 9 12:17:30 CEST 2014


>
> I think manipulating this in string is also wrong on some levels.
>
> What you need is a function that eats away the value but still does it in a float compute,
> like
>
> function EatFloatBelow(Num : double; DecimalPlaces: Integer; EatSign:boolean=true): double;
> var Temp : Integer;
> begin
>    (* Preprocess the float for displaying with fixed decimal places
>     * if Num is smaller than 10**DecimalPlaces, it should also eat the sign by default
>     * WARNING some of this function is pseudocode please check for proper variables *)
>
> (* DecimalPlaces is supposed to be positive *)
>    if DecimalPlaces < 0 then exit;
>
>    (* preflight checks : check that DecimalPlaces is sane *)
>    if 10**(-DecimalPlaces) < DoublePrecision then exit;  (* or Power(10,(-DecimalPlaces)) *)
>
>    Temp := RoundDown(Num*(10**DecimalPlaces)); // first 'shift it' left and get rid of the remaining decimals by rounding down to integer
>    Result := Temp / (10**DecimalPlaces);   // now restore the proper order and back to float
>
>    (* can also combine the above 2 steps and use one less variable
>     * Result := Trunc(Num*(10**DecimalPlaces)) / (10**DecimalPlaces) ; *)
>
>    (* if we ended with something close to zero but still negative, eat the sign *)
>    if (Result < 0) and EatSign and (Result < 10**(-DecimalPlaces))  then
>      Result := Result * (-1) ;
>
> end;
>
> then use
> label1.Caption:= FormatFloat('0.###', EatFloat(strtofloat(edit1.Text),3));
>
> -L.
>

Dear Lukasz,
thanks, anyway I reported this problem using formatfloat just because it 
has the same behaviour of TFloatField.DisplayFormat and it was easier to 
provide a sample code. I do not use FormatFloat so I cannot use your 
function.

As I wrote I think that this behaviour is a bug and not a feature (see 
excel and libreoffice behaviour). Someone can check the delphi behaviour?

Andrea





More information about the Lazarus mailing list