Refactored formatNumber function