admin管理员组

文章数量:1391969

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$.

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$.

Share Improve this question asked Jan 13, 2011 at 16:56 DarkLeafyGreenDarkLeafyGreen 70.5k136 gold badges391 silver badges616 bronze badges 1
  • 1 Maybe try writing this yourself and then ask a specific question if you're having a problem with it? – Brian Donovan Commented Jan 13, 2011 at 16:58
Add a ment  | 

3 Answers 3

Reset to default 3

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

http://weblogs.asp/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 mas, 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 ma, 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 + "$";

本文标签: Format currency using javascriptStack Overflow