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

8
  • And what your function "ConvertNumber" should do?
    – polin
    Commented Nov 22, 2012 at 5:52
  • @polin that is what I wanted Commented Nov 22, 2012 at 5:54
  • @Alex link is for java functions and not able to understand Commented Nov 22, 2012 at 5:56
  • 1. What do you need to return if the value is 100 and the format is #,###.0? 2. Do you use only the given two formats?
    – prageeth
    Commented Nov 22, 2012 at 6:29
  • what do you want in return for 10000000... is 10,000,000 or 10000,000...??
    – Naz Kazi
    Commented Nov 22, 2012 at 6:34

3 Answers 3

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"});
});
5
  • Different locales have different formatting, use the locale that suits your needs, most commonly en for English.
    – elclanrs
    Commented Nov 22, 2012 at 6: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
    Commented Nov 22, 2012 at 6: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
    Commented Nov 22, 2012 at 6:06
  • yes @BlackCobra I also thinking the same need some jQuery or Javascript function for this Commented Nov 22, 2012 at 6:44
  • mredkj.com/javascript/nfbasic.html i got this but not getting much luck Commented Nov 22, 2012 at 6:47
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...

3
  • 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. Commented Nov 22, 2012 at 8: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 Kazi
    Commented Nov 22, 2012 at 8: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 Kazi
    Commented Nov 22, 2012 at 9:11
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'
1
  • Thanks @Palash Mondal for this Commented Nov 22, 2012 at 9:37

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.