最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - LocalString method for german currency? - Stack Overflow

programmeradmin7浏览0评论
var n = (1223578.00).toLocaleString('de-DE');

Should be -> "1.223.578,00", but the output is: 1.223.578 (the decimal places are missing)

Whats wrong ?

<script type="text/javascript">var amount= '{{currency1}}';</script>

this is a filled form from SQL, currency1 = 1223578.00

How can I use this, with this placeholders?

var n = (1223578.00).toLocaleString('de-DE');

Should be -> "1.223.578,00", but the output is: 1.223.578 (the decimal places are missing)

Whats wrong ?

<script type="text/javascript">var amount= '{{currency1}}';</script>

this is a filled form from SQL, currency1 = 1223578.00

How can I use this, with this placeholders?

Share Improve this question edited May 5, 2015 at 13:53 user3652830 asked May 5, 2015 at 13:16 user3652830user3652830 811 silver badge8 bronze badges 1
  • 2 Numbers do not have significant digits. That is your issue. Look at the options in the docs: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – epascarello Commented May 5, 2015 at 13:26
Add a ment  | 

1 Answer 1

Reset to default 9

Set the minimumFractionDigits property to your desired number of digits.

var n = (1223578.00).toLocaleString('de-DE',{ minimumFractionDigits: 2 });

Produces: 1.223.578,00

The above function may not work on all browsers. To ensure the same result on all browsers, you can manually create the string as follows:

Modified From: https://stackoverflow./a/16157942/4347337

function DelocaleString(x, sep, grp, dec) {
  //add decimals
  var y = x.toFixed(dec);
  //strip decimals
  var x_integer = y.split('.')[0];
  var x_fraction = y.split('.')[1];
  var x_fractionString = "";
  if (dec > 0) {
    x_fractionString = ',' + x_fraction;
  }
  var sx = ('' + x_integer).split('.'),
    s = '',
    i, j;
  sep || (sep = ' '); // default seperator
  grp || grp === 0 || (grp = 3); // default grouping
  i = sx[0].length;
  while (i > grp) {
    j = i - grp;
    s = sep + sx[0].slice(j, i) + s;
    i = j;
  }
  s = sx[0].slice(0, i) + s;
  sx[0] = s;
  return sx.join('.') + x_fractionString
}

Example

$().ready(function() {
  $('#convertBtn').click(function() {
    ConvertNumber();
  });
});

function ConvertNumber() {
  var input = $('#in').val();
  if ($.isNumeric(input)) {
    $('#out').val(DelocaleString(+input, '.', 3, 2));
  } else {
    $('#out').val('Input is not a number!');
  }
}

function DelocaleString(x, sep, grp, dec) {
  //add decimals
  var y = x.toFixed(dec);
  //strip decimals
  var x_integer = y.split('.')[0];
  var x_fraction = y.split('.')[1];
  var x_fractionString = "";
  if (dec > 0) {
    x_fractionString = ',' + x_fraction;
  }
  var sx = ('' + x_integer).split('.'),
    s = '',
    i, j;
  sep || (sep = ' '); // default seperator
  grp || grp === 0 || (grp = 3); // default grouping
  i = sx[0].length;
  while (i > grp) {
    j = i - grp;
    s = sep + sx[0].slice(j, i) + s;
    i = j;
  }
  s = sx[0].slice(0, i) + s;
  sx[0] = s;
  return sx.join('.') + x_fractionString
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn./bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn./bootstrap/3.3.6/js/bootstrap.min.js"></script>

<form class="form-horizontal" style="max-width:350px;">
  <div class="form-group">
    <label for="in" class="col-sm-2 control-label">Input</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" id="in" placeholder="Allowable formats: 123.456789, 123456789">
    </div>
  </div>
  <div class="form-group">
    <label for="out" class="col-sm-2 control-label">Output</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" id="out" placeholder="Formatted Number" readonly>
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button class="btn btn-default" id="convertBtn">Convert Number</button>
    </div>
  </div>
</form>

发布评论

评论列表(0)

  1. 暂无评论