[Scilab-users] comma format

Samuel Gougeon sgougeon at free.fr
Sat May 16 16:36:52 CEST 2015


Hello Rafael,

Le 16/05/2015 15:08, Rafael Guera a écrit :
>
> Fyi, a basic way to insert 'commas' by handling the number as a 
> string, in case it helps:
>
> x=-%pi*1e10;
>
> sx=msprintf("%.4f",x); ///define decimal accuracy needed/
>
> N=length(sx);
>
> ifN<4 then return;
>
> end
>
> ix= strindex(sx,'.');
>
> if_isempty_(ix) then ix=N+1;
>
> end
>
> sy=part(sx,1);
>
> fori=2:ix-4
>
> sy = sy+part(sx,i);
>
> if _modulo_(ix-1-i,3)==0 then sy = sy+','; /// comma separator added/
>
> end
>
> end
>
> sy= sy+part(sx,ix-3:N);/// No commas inserted after decimal point/
>
> printf("%s\n%s\n",sx,sy)
>
Not so basic... A new C option for thousands separator could be useful.
Hereafter is another implementation.
Best regards
Samuel

x   =  -%pi*1e10;
sx  =  msprintf("%.4f",abs(x))  //define decimal accuracy needed
if  x<0,  s  =  "-";  else  s  =  "";  end
if  abs(x)<1000  then
     sy  =  s+sx
else
     dp  =  tokens(sx,  ".");
     if  size(dp,1)==1,  dp(2)  =  "";  else  dp(2)  =  "."+dp(2);  end
     L  =  length(dp(1));
     sy  =  s  +  strcat(strsplit(dp(1),  L-(fix(L/3):-1:1)*3),",")  +  dp(2);
end
printf("%s\n%s\n",s+sx,sy)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20150516/3883f16e/attachment.htm>


More information about the users mailing list