Author Topic: [SNIPPET] Number Formatting  (Read 706 times)

I've noticed Blockland does not have number formatting, as far as I know anyway. So I've decided to create a script because I really needed numbers to be formatted. I'm releasing a basic version of the script I've created. I'm sure someone could come up with something better that would support more.

It does not support anything like 10000.00.

Command: FormatNumber(%num, %thousands_sep, %appendedStart);

%thousands_sep is optional
%appendedStart is optional

Example Usage: FormatNumber(900000, ",", "$");

Code: [Select]
function FormatNumber(%num, %thousands_sep, %appendedStart) {
   %num = StripMLControlChars(%num);
   %num = strReplace(%num, ",", "");
   if(%thousands_sep $= "")
      %thousands_sep = ",";   
   
   if(strLen(%num) == 4) {
      %first = getSubStr(%num, 0, 1);
      %last = getSubStr(%num, 1, 3);
     
      return %first @ %thousands_sep @ %last;
   }else if(strLen(%num) == 5) {
      %first = getSubStr(%num, 0, 2);
      %last = getSubStr(%num, 2, 3);
     
      return %first @ %thousands_sep @ %last;
   }else if(strLen(%num) == 6) {
      %first = getSubStr(%num, 0, 3);
      %last = getSubStr(%num, 3, 5);
     
      return %first @ %thousands_sep @ %last;
   }else if(strLen(%num) == 7) {
      %first = getSubStr(%num, 0, 1);
      %middle = getSubStr(%num, 1, 3);
      %last = getSubStr(%num, 4, 6);
     
      return %appendedStart @ %first @ %thousands_sep @ %middle @ %thousands_sep @ %last;
   }else if(strLen(%num) == 1 || strLen(%num) == 2 || strLen(%num) == 3) {
      return %appendedStart @ %num;
   } else {
      return "Error Formatting Number!";
   }
}

could probably do it by a backwards loop through the word starting at a . if one is detected

edit:
there's probably another method that involves dividing by ten but w/e
« Last Edit: July 15, 2012, 05:40:41 PM by otto-san »

There is a much simpler way of doing this I'm absolutely sure of it.
Edit: also it looks like this won't support any sized number
« Last Edit: July 15, 2012, 05:06:19 PM by Ipquarx »

Yes there is

mFloatLength(number,number of decimals);

==> echo(mFloatLength(4,2));
4.00

Yes there is

mFloatLength(number,number of decimals);

==> echo(mFloatLength(4,2));
4.00

You did not read OP. He wants a way to emulate PHP's number_format

I was specifically replying to this line:
It does not support anything like 10000.00.
because that's not true, as evident by my post

i don't understand what this functions does nor what it's practical use is.

could someone explain?

i don't understand what this functions does nor what it's practical use is.

could someone explain?
Can make the numbers look prettier on the user end of something.

i.e. rather than having $1000000 you would have $1,000,000