How can 1000000 be formatted as 1,000,000 ?

 

Hi

Besides Hanover's (free) Handy Utility - NumberToStr() - is there any other way to format numbers so that thousands and millions etc are automatically separated by commas, ie. 1,000,000.

Handy Utility - NumberToStr() accomplishes said objective but poses problems to lengthy to go into.

Thank in advance.

 
IIRC, this question was asked before and two really good programmers provided their versions of how to do it. Search Google for "forum.mql4.com formatting numbers commas" or something along those lines.
 
ubzen:
Search Google for "forum.mql4.com formatting numbers commas" or something along those lines.
Such as NumbersSeparator() function for Print big numbers - MQL4 forum
 
Yep, thats the one. I remember attempting to make that separator. I trashed mines shortly after because it was not on par. Nice educational codes tho.
 
ubzen:
IIRC, this question was asked before and two really good programmers provided their versions of how to do it. Search Google for "forum.mql4.com formatting numbers commas" or something along those lines.

Thanks - google searches - personalized on or off - appear to becoming narrower by the day. Numerous variations of search phrases used return the same results.

Here is one I have come across since

formatDouble

string formatDouble(double number, int precision, string pcomma=",", string ppoint=".")
{
   string snum   = DoubleToStr(number,precision);
   int    decp   = StringFind(snum,".",0);
   string sright = StringSubstr(snum,decp+1,precision);
   string sleft  = StringSubstr(snum,0,decp);
   string formated = "";
   string comma    = "";
   
      while (StringLen(sleft)>3)
      {
         int    length = StringLen(sleft);
         string part   = StringSubstr(sleft,length-3,0);
              formated = part+comma+formated;
              comma    = pcomma;
              sleft    = StringSubstr(sleft,0,length-3);
      }
      
      if (sleft!="")   formated = sleft+comma+formated;
      if (precision>0) formated = formated+ppoint+sright;
   return(formated);
}  
 
Looks kind of similar to one within the link above.
 
Thanks, very helpful - above is one I have come across since.
 
ubzen:
Looks kind of similar to one within the link above.


Yes - they all do - to be honest, my level of put two-and-two together does not qualify to comment as I do not know specifically how they are similar or differ - but it will be interesting comparing code and researching an understanding.

In the meantime I will have a jolly time compiling and testing in the morning.

 
file45:

Thanks - google searches - personalized on or off - appear to becoming narrower by the day. Numerous variations of search phrases used return the same results.

Here is one I have come across since

formatDouble

- formatDouble, copied and pasted from sources, was found missing a line of code resulting in negative numbers being formatted as -,xxx.00 i.e. -,123.45.

The below code rectifies the above incorrect formatting.

formatDouble

string formatDouble(double number, int precision, string pcomma=",", string ppoint=".")
{
   string snum   = DoubleToStr(number,precision);
   int    decp   = StringFind(snum,".",0);
   string sright = StringSubstr(snum,decp+1,precision);
   string sleft  = StringSubstr(snum,0,decp);
   string formated = "";
   string comma    = "";
   
      while (StringLen(sleft)>3)
      {
         int    length = StringLen(sleft);
         string part   = StringSubstr(sleft,length-3,0);
              formated = part+comma+formated;
              comma    = pcomma;
              sleft    = StringSubstr(sleft,0,length-3);
      }
      if (sleft=="-")  comma=""; // this line missing previously
      if (sleft!="")   formated = sleft+comma+formated;
      if (precision>0) formated = formated+ppoint+sright;
   return(formated);
}  
Reason: