Formatting large numbers

How do I format number in millions (1.1M etc). I learned that I can use NumToStr and will get a comma separated number. right now I am using a if condition

if( marketCap[i]> 1000000 )
{
mktcap = NumToStr(marketCap/1000000,1.2) + " T";
}

is there any better way to do this

You might use StrFormat instead.

marketCap = 1234567;

mktcap = StrFormat("%1.2fM", marketCap/1e6);

Or

function StrFormatMillion(value) {
   cond = value>=1e6;
   fmt = WriteIf(cond, "%1.2fM", "%g");
   denom = IIf(cond, 1e6, 1);
   result = StrFormat(fmt, value/denom);
   return result;
}

marketCap = 1234567;
fmt = StrFormatMillion(marketCap);

PS: Another must read here:

Thanks @fxshrat for your quick response.
looks like the Strformat ("%1.2fM") will only format till millions however large number is(Billions/Trillions). so I need to still use the conditional formatting that I current use . Please correct me If I am wrong.

I would prefer something like this '1B' instead of '1000M'. I current achieve this but want to know if there is a better way

You said this originally:


Anyway....

Then do this

function StrFormatLargeNum(value) {
	if ( value<1e6 ) {
		fmt = "%g";
		denom = 1;
	} else if ( value < 1e9 ) {
		fmt = "%1.2fM";
		denom = 1e6;
	} else if ( value < 1e12 ) {
		fmt = "%1.2fB";
		denom = 1e9;		
	} else {
		fmt = "%1.2fT";
		denom = 1e12;	
	}	
	return  StrFormat(fmt, value/denom);
}

marketCap = 1234567;
fmt = StrFormatLargeNum(marketCap);
7 Likes