Browse Source

adjust round

MaiXinRong 8 years ago
parent
commit
348e946d84
1 changed files with 17 additions and 1 deletions
  1. 17 1
      Units/UtilMethods.pas

+ 17 - 1
Units/UtilMethods.pas

@@ -182,6 +182,19 @@ begin
     Result := CommonRoundTo(AValue, iTotalPriceDigit);
 end;
 
+function GetTrueDigit(AValue: Double): Integer;
+var
+  sValue: string;
+  iPointPos: Integer;
+begin
+  sValue := FloatToStr(AValue);
+  iPointPos := Pos('.', sValue);
+  if iPointPos = 0 then
+    Result := 0
+  else
+    Result := -(Length(sValue) - iPointPos);
+end;
+
 function CommonRoundTo(AValue: Double; ADigit: Integer; RoundMode: TRoundMode = rmNearest): Double;
 var
   X: Double;
@@ -204,7 +217,10 @@ begin
     end;
   P := @X;
   CopyMemory(P, @Buf[0], SizeOf(X));
-  Result := InnerRoundTo(X, ADigit, RoundMode);
+  if (ADigit > 0) then
+    Result := InnerRoundTo(X, Min(GetTrueDigit(AValue), ADigit), RoundMode)
+  else
+    Result := InnerRoundTo(X, Max(GetTrueDigit(AValue), ADigit), RoundMode);
 end;
 
 {Interface Control}