plastikaweb
11/6/2017 - 9:46 AM

JS Bin format currency higher order function // source http://jsbin.com/mixasix

JS Bin

format currency higher order function

// source http://jsbin.com/mixasix

<!DOCTYPE html>
<html>
<head>
<meta name="description" content="format currency higher order function">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>

<script id="jsbin-javascript">
'use strict';

var formatCurrency = function formatCurrency(currencySymbol, decimalSeparator) {
  return function (value) {
    var wholePart = Math.trunc(value / 100);
    var fractionPart = value % 100;
    if (fractionPart < 10) fractionPart = '0' + fractionPart;
    return '' + currencySymbol + wholePart + decimalSeparator + fractionPart;
  };
};

var format = formatCurrency('€', ',');

console.log(format(0));
console.log(format(3244));
</script>



<script id="jsbin-source-javascript" type="text/javascript">const formatCurrency = function( currencySymbol, decimalSeparator ) {
  return function( value ) {
    const wholePart = Math.trunc( value / 100 );
    let fractionPart = value % 100;
    if ( fractionPart < 10 ) fractionPart = '0' + fractionPart;
    return `${currencySymbol}${wholePart}${decimalSeparator}${fractionPart}`;
  }
}

const format = formatCurrency('€', ',');

console.log(format(0));
console.log(format(3244));</script></body>
</html>
'use strict';

var formatCurrency = function formatCurrency(currencySymbol, decimalSeparator) {
  return function (value) {
    var wholePart = Math.trunc(value / 100);
    var fractionPart = value % 100;
    if (fractionPart < 10) fractionPart = '0' + fractionPart;
    return '' + currencySymbol + wholePart + decimalSeparator + fractionPart;
  };
};

var format = formatCurrency('€', ',');

console.log(format(0));
console.log(format(3244));