6

I got requirement of the java-script Number formatting like below

I need one function its having 2 parameters like

var number = 1000;
var format = #,###.0; //or #,### ; 

function ConvertNumber(number, format){   
    // this function need to return 1,000.0 if format is #,###.0
    // this function need to return 1,000 if format is #,### 
}

can anyone having this kind of function? your help is really appreciated thanks

Ayush
  • 41,754
  • 51
  • 164
  • 239
Rohan Patil
  • 1,865
  • 1
  • 23
  • 36

3 Answers3

3

You can se the NumberFormatter plug-in from JQuery.

Here's an example of how you'd use this plugin.

$("#salary").blur(function(){
    $(this).format({format:"#,###.00", locale:"de"});
});
Frank
  • 16,476
  • 7
  • 38
  • 51
  • Different locales have different formatting, use the locale that suits your needs, most commonly `en` for English. – elclanrs Nov 22 '12 at 06:02
  • From the documentation: a number that we would write in the US as "1,250,500.75" would be written differently in different countries: "1.250.500,75" in Germany, "1 250 500,75" in France, and "1'250'500.75" in Switzerland, and "125,0500.75" in Japan. The number is exactly the same, but it's just written using a different format when presented to users of the web application. – Frank Nov 22 '12 at 06:03
  • Using locale is not advised, you don't know if the user has access to locality settings or understands them, or whether they are used to the browser they are using presenting numbers in their "local" format. Better to use a format you understand that is explained to users or is unambiguous. – RobG Nov 22 '12 at 06:06
  • yes @BlackCobra I also thinking the same need some jQuery or Javascript function for this – Rohan Patil Nov 22 '12 at 06:44
  • http://www.mredkj.com/javascript/nfbasic.html i got this but not getting much luck – Rohan Patil Nov 22 '12 at 06:47
1

Try this:

function ConvertNumber(number, decimals, dec_point, thousands_sep) {

  // Strip all characters but numerical ones.
  number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
  var n = !isFinite(+number) ? 0 : +number,
    prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
    sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
    dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
    s = '',
    toFixedFix = function (n, prec) {
      var k = Math.pow(10, prec);
      return '' + Math.round(n * k) / k;
    };

  // Fix for IE parseFloat(0.55).toFixed(0) = 0;
  s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
  if (s[0].length > 3) {
    s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
  }
  if ((s[1] || '').length < prec) {
    s[1] = s[1] || '';
    s[1] += new Array(prec - s[1].length + 1).join('0');
  }
  return s.join(dec);
}​

Example:

example 1: ConvertNumber(1234.5678, 2, '.', ',');
returns 1: '1,234.57'

example 2: ConvertNumber(1000);
returns 2: '1,000'

example 3: ConvertNumber(1000.55, 1);
returns 3: '1,000.6'

example 4: ConvertNumber(67000, 5, ',', '.');
returns 4: '67.000,00000'

example 5: ConvertNumber('1.20', 4);
returns 5: '1.2000'
palaѕн
  • 72,112
  • 17
  • 116
  • 136
1

check this function...

var number = 1000; // int or float or string
var format = '#,###.0'; // you can put any format that you want... only string

function ConvertNumber(number, format){
    var tail=format.lastIndexOf('.');number=number.toString();
    tail=tail>-1?format.substr(tail):'';
    if(tail.length>0){if(tail.charAt(1)=='#'){
        tail=number.substr(number.lastIndexOf('.'),tail.length);
    }}
    number=number.replace(/\..*|[^0-9]/g,'').split('');
    format=format.replace(/\..*/g,'').split('');
    for(var i=format.length-1;i>-1;i--){
        if(format[i]=='#'){format[i]=number.pop()}
    }
    return number.join('')+format.join('')+tail;
}

// Examples
// ConvertNumber(1234,'#,###') === 1,234
// ConvertNumber(1234,'#,###.00') === 1,234.00
// ConvertNumber(1234,',###.00') === 1,234.00
// ConvertNumber(1234.4575,',###.##') === 1,234.45
// ConvertNumber(1234567890.4575,',###,###,###.##') === 1,234,567,890.45
// ConvertNumber(123.4575,',#,#.##') === 1,2,3.45
// ConvertNumber(123456.4575,'-#-#.##') === 1234-5-6.45
// ConvertNumber(1234567.4575,' ## ###.##') === 12 34 567.45

hope this function will be very helpful for you and them who have the same problem... best of luck...

Naz
  • 2,520
  • 2
  • 16
  • 23
  • its really acceptable thanks for this can you do this with this function if number is <1000 or >9999 then I want to do the normal number formatting for eg: 100000 is convert to 1,00,000 etc. – Rohan Patil Nov 22 '12 at 08:46
  • then help me to update the function... dear I don't know what is the normal format for your country... please format "1 000 000 000 000 000.000 000" in your normal format... – Naz Nov 22 '12 at 08:51
  • normal formatting is also possible with this function... but first you have to show me a long number example on normal formatting... then after updating the function if you don't mention any `format` the `number` will be formatted according to normal formatting... – Naz Nov 22 '12 at 09:11