Custom separators

File: custom_separators.html

<!DOCTYPE html>
  <title>math.js | custom separators</title>
    body, input, select {
      font: 11pt sans-serif;
    input, select, th, #result {
      padding: 5px 10px;
    th {
      text-align: left;

  <script src=""></script>

  This code example shows how to apply custom separators for function arguments and decimal separator.

    <th>Argument separator</th>
      <input id="args" type="text" value=";">
    <th>Decimal separator</th>
      <input id="decimals" type="text" value=",">
      <input id="expression" type="text" value="sum(3,4; 2,1; 2,0)" />
      <input id="evaluate" type="button" value="Evaluate">
    <td id="result"></td>

  // pointers to the input elements
  var expression = document.getElementById('expression');
  var evaluate   = document.getElementById('evaluate');
  var result     = document.getElementById('result');
  var args       = document.getElementById('args');
  var decimals   = document.getElementById('decimals');

  // attach event handler to the button
  evaluate.onclick = function () {
    // replace the custom separators in the input with the default separators
    var expr = expression.value
        .replace(new RegExp('\\' + decimals.value + '|\\' + args.value, 'g'), function (match) {
          return match == decimals.value ? '.': ',';

    // do the actual evaluation
    var res = math.eval(expr);

    // replace the default separators in the output with custom separators
    result.innerHTML = res.toString()
        .replace(new RegExp(',|\\.', 'g'), function (match) {
          return match == '.' ? decimals.value : args.value;

Fork me on GitHub