Browse Source

adjust round 2

MaiXinRong 8 years ago
parent
commit
9611d5652d
1 changed files with 8 additions and 9 deletions
  1. 8 9
      Units/UtilMethods.pas

+ 8 - 9
Units/UtilMethods.pas

@@ -183,16 +183,15 @@ begin
 end;
 
 function GetTrueDigit(AValue: Double): Integer;
+const
+  sFormat12 = '0.############';
 var
-  sValue: string;
+  sValue, sDigitValue: string;
   iPointPos: Integer;
 begin
-  sValue := FloatToStr(AValue);
+  sValue := FormatFloat(sFormat12, AValue);
   iPointPos := Pos('.', sValue);
-  if iPointPos = 0 then
-    Result := 0
-  else
-    Result := -(Length(sValue) - iPointPos);
+  Result := Min(0, -(Length(sValue) - iPointPos));
 end;
 
 function CommonRoundTo(AValue: Double; ADigit: Integer; RoundMode: TRoundMode = rmNearest): Double;
@@ -217,10 +216,10 @@ begin
     end;
   P := @X;
   CopyMemory(P, @Buf[0], SizeOf(X));
-  if (ADigit > 0) then
-    Result := InnerRoundTo(X, Min(GetTrueDigit(AValue), ADigit), RoundMode)
+  if (ADigit < 0) and (ADigit < GetTrueDigit(AValue)) then
+    Result := AValue
   else
-    Result := InnerRoundTo(X, Max(GetTrueDigit(AValue), ADigit), RoundMode);
+    Result := InnerRoundTo(X, ADigit, RoundMode);
 end;
 
 {Interface Control}