Number separator using commas
-
I've searched the forum but unable to see a way to format numbers using commas as separators when adding to comments on screen.
For example 34567 would display as 34,567
Does anyone know if there is a built in function to achieve this or have a code snippet for such a function.
Any help appreciated,
Thank you
-
@mantadiver The only way I know of is dividing the number by 1,000 before showing it on screen.
-
Managed to incorporate the "NumberToString" function put forward by nicholish en at end of page one of this mql5 forum thread 135250 (Google: NumbersSeparator() function for Print big numbers).
This allows numbers to be automatically formatted with commas by using a new function:
NumberToString ( numbertoconvert , decimalplaces )- Create Custom Block
- Enter "NumberToString" as new block title in "new block..." field
- Look for pane "Global variables, includes" at bottom of page
- Type in "#include<Strings\String.mqh>"
- Look for pane in bottom right titled "Custom Functions" press "new"
- Remove the default text
- Paste in the code supplied by nicholish en:
//+------------------------------------------------------------------+
#include<Strings\String.mqh>
template<typename T>
string NumberToString(T number,int digits = 0,string sep=",")
{
CString num_str;
string prepend = number<0?"-":"";
number=number<0?-number:number;
int decimal_index = -1;
if(typename(number)=="double" || typename(number)=="float")
{
num_str.Assign(DoubleToString((double)number,digits));
decimal_index = num_str.Find(0,".");
}
else
num_str.Assign(string(number));
int len = (int)num_str.Len();
decimal_index = decimal_index > 0 ? decimal_index : len;
int res = len - (len - decimal_index);
for(int i = res-3;i>0;i-=3)
num_str.Insert(i,sep);
return prepend+num_str.Str();
}
//+------------------------------------------------------------------+- Press "save" & close window
- Press "save changes" at top of custom blocks screen
- Return to fxdreema builder, refresh browser window and add new custom block in Oninit
Then whenever number formats using commas are required use:-
NumberToString ( numbertoconvert , decimalplaces )
Output will show as 1,234,567 rather than 1234567.