Bläddra i källkod

* handle rounding of currency values correctly, resolves #12207

git-svn-id: trunk@14438 -
florian 15 år sedan
förälder
incheckning
d3cd6132f2
3 ändrade filer med 35 tillägg och 2 borttagningar
  1. 1 0
      .gitattributes
  2. 21 2
      rtl/inc/sstrings.inc
  3. 13 0
      tests/webtbs/tw12207.pp

+ 1 - 0
.gitattributes

@@ -9985,6 +9985,7 @@ tests/webtbs/tw12137.pp svneol=native#text/plain
 tests/webtbs/tw12151.pp svneol=native#text/plain
 tests/webtbs/tw12186.pp svneol=native#text/plain
 tests/webtbs/tw12202.pp svneol=native#text/plain
+tests/webtbs/tw12207.pp svneol=native#text/pascal
 tests/webtbs/tw12214.pp svneol=native#text/plain
 tests/webtbs/tw1222.pp svneol=native#text/plain
 tests/webtbs/tw12224.pp svneol=native#text/plain

+ 21 - 2
rtl/inc/sstrings.inc

@@ -500,12 +500,12 @@ procedure fpc_shortstr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out
 procedure fpc_shortstr_currency(c : currency; len,f : SizeInt; out s : shortstring);[public,alias:'FPC_SHORTSTR_CURRENCY']; compilerproc;
 const
   MinLen = 8; { Minimal string length in scientific format }
-
 var
   buf : array[1..19] of char;
   i,j,k,reslen,tlen,sign,r,point : longint;
   ic : qword;
 begin
+  fillchar(buf,length(buf),'0');
   { default value for length is -32767 }
   if len=-32767 then
     len:=25;
@@ -594,7 +594,26 @@ begin
             break;
         end;
       If (k=1) and (buf[i-1]='0') then
-        buf[i]:=chr(Ord(buf[i])+1);
+	    begin
+		  { 1.9996 rounded to two decimal digits after the decimal separator must result in
+		    2.00, i.e. the rounding is propagated
+		  }
+          while buf[i]='9' do
+		    begin
+			  buf[i]:='0';
+     		  inc(i);
+		    end;
+		  buf[i]:=chr(Ord(buf[i])+1);
+		  { did we add another digit? This happens when rounding
+		    e.g. 99.9996 to two decimal digits after the decimal separator which should result in
+			100.00
+		  }
+		  if i>reslen then
+		    begin
+			  inc(reslen);
+			  inc(tlen);
+			end;
+		end;		  
     end;
   { preparing result string }
   if reslen<len then

+ 13 - 0
tests/webtbs/tw12207.pp

@@ -0,0 +1,13 @@
+uses
+  sysutils;
+var
+  s : string;
+begin
+  str(currency(25.996):0:2,s);
+  if s<>'26.00' then
+    halt(1);
+  str(currency(99.996):0:2,s);
+  if s<>'100.00' then
+    halt(1);
+  writeln('ok');
+end.