Jump to content

Recommended Posts

Hi there,

 

Today I ran across a problem, I wanted to format different currencies, but due to the lack of inbuilt maths and decent formatting functions and of course most formatting uses scientific notation which is terribly useless for user-end.

 

So I've put together a function, I have only just quickly thrown this together just now, so I have not really bothered to look at ways to optimise it yet and due to limitations, precision is still lost over specific lengths, but it is still ideal for most scenarios, I will take a look into the precision problem at a later date.

 

A few examples:

 

"$" + ([345435.21, 2, ".", ","] call PX_fnc_formatNumber); //Output: $345,435.22


 

"£" + ([1343345435.21, 2, ".", ","] call PX_fnc_formatNumber); //Output: £1,343,345,408.00 - notice that precion is lost



How about the Bulgarian currency (leva)?
 

format["%1%2",([113225.21, 2, ",", " "] call PX_fnc_formatNumber), " лв"]; //Output: 113 225,21 лв

 

And in use:

max_750x750.jpg

 

 

You can grab the function from HERE, meanwhile, if anyone already knows a solution for the precision, please let me know :)

Share this post


Link to post
Share on other sites

Strings are impossible to work with when dealing with a money system for example, so it would still rely on passing a toFixed param to strNumFormat function you posted unfortunately :(

 

I have also just tested, and my function is slightly slower due to additional parameters, but if i remove parameters and have them hardcoded (similar to the one you posted) - then my function is approx 150 ms faster than KK_fnc_strNumFormat on 10000 iterations.

//
// Format a float-point number. e.g. 5233255.12 => 5,233,255.12
// Copyright (c) Colin J.D. Stewart. All rights reserved.
// Free for use with credit to author.
// Example: _number call PX_fnc_formatNumberEx;
//
#define PX_DC_SEP "."
#define PX_TH_SEP ","
#define PX_DC_PL 2

PX_fnc_formatNumberEx = {
	private ["_arr", "_str", "_count"];
	
	_str = "";
	_count = 0;
	
	_arr = toArray (_this toFixed PX_DC_PL);
	reverse _arr;	
	{		
		if (_forEachIndex == PX_DC_PL) then {
			_str = PX_DC_SEP + _str;
		} else {
			if (_forEachIndex < PX_DC_PL) then {
				_str = toString[_x] + _str;
			} else {
				if (_count == 3) then {
					_count = 0;
					_str = PX_TH_SEP + _str;
				};
					
				_str = toString[_x] + _str;
				_count = _count + 1;			
			};
		};
	} forEach _arr;

	_str;
};

 

slightly faster:

//
// Format a float-point number. e.g. 5233255.12 => 5,233,255.12
// Copyright (c) Colin J.D. Stewart. All rights reserved.
// Free for use with credit to author.
// Example: _number call PX_fnc_formatNumberEx;
//
#define PX_DC_SEP "."
#define PX_TH_SEP ","
#define PX_DC_PL 2

PX_fnc_formatNumberEx = {
    private ["_arr", "_str", "_count"];
        
    _count = 0;
    _arr = (_this toFixed PX_DC_PL) splitString ".";
    _str = "."+(_arr select 1);
    _arr = toArray(_arr select 0);
    reverse _arr;    
    
    {        
        if (_count == 3) then {
            _count = 0;
            _str = PX_TH_SEP + _str;
        };
                    
        _str = toString[_x] + _str;
        _count = _count + 1;            
    } forEach _arr;

    _str;
};

 

I will look later to improve and optimise further. (i have a few more ideas)

  • Like 1

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×