Format an Amount as Currency

To add comments, please log in or register
Brian Lillard
7242
Brian Lillard  

Hello all

First I am sharing a code I wrote to format currency amounts with commas.


Secondly I could not find this anywhere in the documentation but I'd like to simplify it.


string sCurrency(string sIn){
 bool bNegative=((double)sIn<0?true:false); if(bNegative){sIn=DoubleToString(MathAbs((double)sIn),2);}
 string sDollars,sCents=StringSubstr(sIn,StringLen(sIn)-3,3),sTmp=StringSubstr(sIn,0,StringLen(sIn)-3);
 for(int iCnt=StringLen(sTmp); iCnt>=0; iCnt--){
  if((iCnt==3 && StringLen(sTmp)!=3) || (iCnt==6 && StringLen(sTmp)!=6) || 
     (iCnt==9 && StringLen(sTmp)!=9) || (iCnt==12 && StringLen(sTmp)!=12))
   {sDollars=sDollars+","+StringSubstr(sTmp,StringLen(sTmp)-iCnt,1);}
  else{sDollars=sDollars+StringSubstr(sTmp,StringLen(sTmp)-iCnt,1);}
  } return((bNegative?"-":"")+sDollars+sCents);
 }
Brian Lillard
7242
Brian Lillard  

Yes awesome ,

I like this one also

https://www.mql5.com/en/forum/135250

Thanks

NumbersSeparator() function for Print big numbers
NumbersSeparator() function for Print big numbers
  • 2011.08.19
  • www.mql5.com
Hi Coders! I'm looking for a little number to string function. I have a number, for example: 344256454...
William Roeder
20248
William Roeder  
Brian Lillard: I like this one also

Same link

To add comments, please log in or register