Round a number to the 1's place. - Node.js Math

Node.js examples for Math:Round

Description

Round a number to the 1's place.

Demo Code

// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Round a number to the 1's place.
function formatNumber(str) {
  str += '';/*from   ww w .  java2 s. c om*/
  if (str == '0') {
    return 'N/A ';
  }
  var x = str.split('.');
  var x1 = x[0];
  var x2 = x.length > 1 ? '.' + x[1] : '';
  var regex = /(\d+)(\d{3})/;
  while (regex.test(x1)) {
    x1 = x1.replace(regex, '$1' + ',' + '$2');
  }
  return x1;
}

Related Tutorials