anchor.diff:      bestcx := Low(cx) + 1 + x
     else          // bestcx > cx maximum
       bestcx := High(cx) - 1 + x;
  -    
  +
     tw   := 0;
     dpos := 0;
     n    := 0;
  
positive.code: 
  procedure TfpgBaseNumericEdit.SetOldColor(const AValue: TfpgColor);
  begin
    if fOldColor=AValue then exit;
    fOldColor:=AValue;
  end;
  
  procedure TfpgBaseNumericEdit.SetAlignment(const AValue: TAlignment);
  begin
    if fAlignment=AValue then exit;
    fAlignment:=AValue;
  end;
  
  procedure TfpgBaseNumericEdit.SetDecimalSeparator(const AValue: char);
  begin
    if fDecimalSeparator=AValue then exit;
    fDecimalSeparator:=AValue;
  end;
  
  procedure TfpgBaseNumericEdit.SetNegativeColor(const AValue: TfpgColor);
  begin
    if fNegativeColor=AValue then exit;
    fNegativeColor:=AValue;
  end;
  
  procedure TfpgBaseNumericEdit.SetThousandSeparator(const AValue: char);
  begin
    if fThousandSeparator=AValue then exit;
    fThousandSeparator:=AValue;
  end;
  
  procedure TfpgBaseNumericEdit.Justify;
  begin
    //based on Alignment property this method will align the derived edit correctly.
  end;
  
  procedure TfpgBaseNumericEdit.HandleKeyChar(var AText: TfpgChar;
    var shiftstate: TShiftState; var consumed: Boolean);
  begin
    inherited HandleKeyChar(AText, shiftstate, consumed);
    Format; // just call format virtual procedure to have a simple way to manage polymorphism here
  end;
  
  procedure TfpgBaseNumericEdit.HandlePaint;
  var
    x: TfpgCoord;
    r: TfpgRect;
  begin
    if Alignment = taRightJustify then
    begin
      Canvas.BeginDraw;
      inherited HandlePaint;
      //  Canvas.ClearClipRect;
      //  r.SetRect(0, 0, Width, Height);
      r.SetRect(2, 2, Width - 4, Height - 4);
      Canvas.SetClipRect(r);
      Canvas.Clear(BackgroundColor);
      Canvas.SetFont(Font);
      Canvas.SetTextColor(TextColor);
      x := Width - Font.TextWidth(Text) - 3;
      Canvas.DrawString(x,3,Text);
      Canvas.EndDraw;
      if Focused then
        fpgCaret.SetCaret(Canvas, x + Font.TextWidth(Text) - 1, 3, fpgCaret.Width, Font.Height);
    end
    else
    inherited;
  end;
  
  procedure TfpgBaseNumericEdit.Format;
  begin
    // Colour negative number
    if LeftStr(Text,1) = '-' then
      TextColor := NegativeColor
    else
      TextColor := OldColor;
  end;
  
  constructor TfpgBaseNumericEdit.Create(AOwner: TComponent);
  begin
    inherited Create(AOwner);
    fAlignment := taRightJustify;
    DecimalSeparator := SysUtils.DecimalSeparator;
    ThousandSeparator := SysUtils.ThousandSeparator;
    NegativeColor := clRed;
    OldColor := TextColor;
  end;
  
  { TfpgEditInteger }
  
  function TfpgEditInteger.GetValue: integer;
  var
    txt: string;
  begin
    if ShowThousand then
    begin
      if Copy(fText, 1, 1) = '-' then
        txt := Copy(ftext, 2, Length(fText) - 1)
      else
        txt := fText;
    	while Pos(ThousandSeparator, txt) > 0 do
    		txt := Copy(txt, 1, Pred(Pos(ThousandSeparator, txt)))
               +Copy(txt, Succ(Pos(ThousandSeparator, txt)), Length(txt) - Pos(ThousandSeparator, txt));
      if Copy(fText, 1, 1) = '-' then
        fText := '-' + txt
      else
        fText := txt;
    end;
    if fText = '-' then
    begin
      Result := 0;
      Text:= fText;
    end
    else
      if Text > '' then
        try
          Result := StrToInt(fText);
        except
          on E: EConvertError do
          begin
            Result := 0;
            Text := '';
            Invalidate;
          end;
        end
      else
        Result := 0;
  end;
  
  procedure TfpgEditInteger.SetValue(const AValue: integer);
  begin
    try
      Text := IntToStr(AValue);
      Format;
    except
      on E: EConvertError do
        Text := '';
    end;
  end;
  
  procedure TfpgEditInteger.SetShowThousand;
  var
  	i,long: integer;
    txt, texte: string;
  begin
    if ShowThousand then
    begin
      if fText > '' then
        if fText[1] = '-' then
          txt:= UTF8Copy(fText, 2, UTF8Length(fText)-1)
        else
          txt:= fText;
    	long := UTF8Length(txt);
    	if long = 0 then
    		texte := ''
    	else
    	begin
        for i := 1 to UTF8Length(txt) do
          if txt[i] = ThousandSeparator then
            Exit;                               // avoids additional separators when pressing return
    		i := 0;
    		texte := '';
    		repeat
    			if i > 0 then
    				if ((i mod 3) = 0) and (txt[UTF8Length(txt)-UTF8Length(texte)] <> ThousandSeparator) then
            begin
    					texte := ThousandSeparator + texte;
              UTF8Insert(texte, txt, FCursorPos + 1);
              if fText[1] = '-' then
              begin
                if Pred(FCursorPos) <= UTF8Length(texte) then
                  Inc(FCursorPos);
              end
              else
                if FCursorPos <= UTF8Length(texte) then
                  Inc(FCursorPos);
            end;
    			texte := Copy(txt, long - i, 1) + texte;
    			inc(i);
    		until i = long;
    	end;
    if fText > '' then
      if fText[1] = '-' then
        fText:= '-' + texte
      else
        fText := texte;
    end;
  end;
  
  procedure TfpgEditInteger.Format;
  begin
    SetShowThousand;
    inherited Format;
  end;
  
  procedure TfpgEditInteger.HandleKeyChar(var AText: TfpgChar;
    var shiftstate: TShiftState; var consumed: Boolean);
  var
    n: integer;
  begin
    n := Ord(AText[1]);
    if ((n >= 48) and (n <= 57) or (n = Ord('-')) and (Pos(AText[1], Self.Text) <= 0)) then
      consumed := False
    else
      consumed := True;
    inherited HandleKeyChar(AText, shiftstate, consumed);
  end;
  
  constructor TfpgEditInteger.Create(AOwner: TComponent);
  begin
    inherited Create(AOwner);
    fShowThousand := True;
  end;
  
  { TfpgEditFloat }
  
  function TfpgEditFloat.GetValue: extended;
  var
    txt: string;
  begin
    if fDecimals > 0 then
    begin
      if Pos(DecimalSeparator, fText) > 0 then
        if UTF8Length(fText)-Pos(DecimalSeparator, fText) > fDecimals then
          fText := Copy(fText, 1, UTF8Length(fText) - 1);
    end
    else
      if fDecimals = 0 then
        if Pos(DecimalSeparator, fText) > 0 then
          fText := Copy(fText, 1, UTF8Length(fText) - 1);
    if ShowThousand then
    begin
      if Copy(fText, 1, 1) = '-' then
        txt := Copy(ftext, 2, Length(fText) - 1)
      else
        txt := fText;
    	while Pos(ThousandSeparator, txt) > 0 do
    		txt := Copy(txt, 1, Pred(Pos(ThousandSeparator, txt)))
               +Copy(txt, Succ(Pos(ThousandSeparator, txt)), Length(txt) - Pos(ThousandSeparator, txt));
      if Copy(fText, 1, 1) = '-' then
        fText := '-' + txt
      else
        fText := txt;
    end;
    if fText = '-' then
    begin
      Result := 0;
      Text:= fText;
    end
    else
      if fText > '' then
      try
        Result := StrToFloat(fText);
      except
        on E: EConvertError do
        begin
          Result := 0;
          Text := '';
          Invalidate;
        end;
      end
    else
      Result := 0;
  end;
  
  procedure TfpgEditFloat.SetValue(const AValue: extended);
  begin
    try
      if FFixedDecimals then
        Text := FloatToStrF(AValue, ffFixed, 18, FDecimals)
      else
        Text := FloatToStr(AValue);
    except
      on E: EConvertError do
        Text := '';
    end;
  end;
  
  procedure TfpgEditFloat.SetShowThousand;
  var
  	i,long: integer;
    txt, texte, decimal: string;
  begin
    if fDecimals > 0 then
      if Pos(DecimalSeparator, fText) > 0 then
      begin
        txt := UTF8Copy(fText, 1, Pred(Pos(DecimalSeparator, fText)));
        if UTF8Length(fText)-Pos(DecimalSeparator, fText) > fDecimals then
          decimal := UTF8Copy(fText, Succ(Pos(DecimalSeparator, fText)), fDecimals)
        else
          decimal := UTF8Copy(fText, Succ(Pos(DecimalSeparator, fText)), UTF8Length(fText)-Pos(DecimalSeparator, fText));
      end
      else
        txt := fText
    else
      if fDecimals = 0 then
        if Pos(DecimalSeparator, fText) > 0 then
          txt := UTF8Copy(fText, 1, Pred(Pos(DecimalSeparator, fText)))
        else
  
negative.code:         FSelOffset := FCursorPos - FSelStart
        else
          StopSelection;
  
        Adjust;
      end;
    end; // movement key checking
  