开发者

Format currency using javascript

a script returns either a number like 0.0580 so in x.xxxx format or a 开发者_运维问答(x) for X units left.

I want to format the number 0.0580 and return 5.8 cent or return x units left.

Any ideas how to do that in javascript? Especially how do I format the x.xxxx?

In case the first x is not 0 I want to return e.g. 1.75$.


MS has written a nice plugin for jquery. it's especially useful if you're localizing. Give it a go:

http://weblogs.asp.net/scottgu/archive/2010/06/10/jquery-globalization-plugin-from-microsoft.aspx

I'm not sure if this can be used outside of jquery...


I may be spoiling you here, but whatever. Here's a function that I found somewhere at some point and have been recycling since. I haven't actually bothered to look much into it to figure out what it does exactly, but it has been rather useful:

function FormatMoneyAmount(starting_string, ending_string) {
  //check validity of input (true = invalid, false = valid)
  var valid_exp = new RegExp ('[^0-9,.$]', 'gi');
  input_invalid = (typeof(ending_string) == 'undefined' && valid_exp.test(starting_string));

  //check if more than 2 digits follow decimal or no decimal
  decimal_invalid = typeof(ending_string) == 'undefined' && (starting_string.indexOf('.') > -1) && ((starting_string.length - starting_string.indexOf('.')) > 3);

  if (input_invalid || decimal_invalid) {
    ending_string = starting_string;
  } else {
    //remove commas, dollar signs
    var replace_exp = new RegExp ('[,$]', 'gi');
    starting_string = starting_string.replace(replace_exp, '');

    //remove decimal if ending string not set, save for adding on later
    var decimal_substring = '';
    if (typeof(ending_string) == 'undefined' && starting_string.indexOf('.') > -1) {
      decimal_substring = starting_string.substring(starting_string.indexOf('.'), starting_string.length);
      remaining_string = starting_string.substring(0,starting_string.indexOf('.'));
    } else {
      remaining_string = starting_string;
    }

    //if string is already 3 characters or less, do nothing
    if (remaining_string.length > 3) {
      //separate last 3 characters of string from rest of string
      var final_three = remaining_string.substring(remaining_string.length - 3, remaining_string.length);
      remaining_string = remaining_string.substring(0, remaining_string.length - 3);

      //if not first group of 3, add new group before old group with comma, else set to new group
      ending_string = (typeof(ending_string) == 'undefined') ? final_three + ((typeof(decimal_substring) == 'undefined') ? '' : decimal_substring) : final_three + ',' + ending_string;

      //call function again if more than 3 digits remaining to process, else add to end string
      if (remaining_string.length > 3) {
        ending_string = FormatMoneyAmount(remaining_string, ending_string);
      } else {
        ending_string = remaining_string + ',' + ending_string;
      }
    } else {
      ending_string = (typeof(ending_string) == 'undefined') ? remaining_string : remaining_string + ',' + ending_string + ((typeof(decimal_substring) == 'undefined') ? '' : decimal_substring);
    }
  }
  return ending_string;
}


The first thing to do is check the format of the string, since you will have two code paths depending on the result:

if (typeof num = "string" && num.slice(0,1) == "(" && num.slice(-1) == ")") {
    // String is in the format (x), so we just need to return that number
    return num.slice(1,-1) + " units left";
}

The next part is to check if the number is less than 1, indicating that it is cents and not whole dollars. If it is less than 1, multiplying it by 100 will give you the number of cents you're after:

if (+num < 1)
    // 0.0580 * 100 = 5.8
    return (num * 100) + " cents";
else
    return +num + "$";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜