0

Suppose you have an array of the following element basically three numbers: 2,000 3,000, and 1,000

In order to say multiply each by 1,000 you would have to parse it to be set as an int:

var i = 0;
var array_nums[3,000, 2,000, 1,000];
for(i = 0; i < array_nums.length; i++){
  array_nums[i] = parseInt(array_nums[i]);
}
arraytd_dth_1[i]*1000;
//arraytd_dth_1[i].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Once parsed I would want to multiply by them by 1,000 and display them so that it has commas for every three digits. This is just a general solution I came up with for Javascript and JQuery.

  • Should be `array_nums = [3,000, 2,000, 1,000]` – Carsten Løvbo Andersen Oct 05 '17 at 18:13
  • I noticed when I parse it 3,000 becomes 3. It should be 3000. Same happens for the others too. – user8723305 Oct 05 '17 at 18:15
  • Your array has six numbers not 3, the `,` is a separator in JavaScript, not a decimal point, use `3.000` instead. – DjaouadNM Oct 05 '17 at 18:20
  • The problem is these elements in the array are coming from an XML file with the values 3,000 2,000 and 1,000. Thus it really is three numbers. The XML file was already formatted that way. – user8723305 Oct 05 '17 at 18:22
  • @user8723305 - you'll need to manipulate the data coming in from the xml in order to end up with the intended array of integers. As is, this array is actually 6 elements ... [3, 0, 2, 0, 1, 0] which would output to ["3,000", "0", "2,000", "0", "1,000", "0"] – jbrown Oct 05 '17 at 18:37
  • Depending on your locale - `2,000` could mean _"Two-Thousand"_ or it could mean _"Two point zero-zero-zero"_ (because in many countries comma is the decimal separator and period is the thousands separator, e.g. `43.210,98`) ... if you mean two-thousand you should write `2000` with no separator, if it's a number, but since it's read from XML I'd guess it's a String, so in your test-case here write it as `var array_nums = ["3,000", "2,000", "1,000"];` an Array-of-Strings. – Stephen P Oct 05 '17 at 18:38
  • depending on how your numbers are derived, you are going to want to supply a radix parameter to parseInt(). https://stackoverflow.com/a/6611854/516492 – jnoreiga Oct 05 '17 at 19:27

3 Answers3

1

2,000 is not a number. It's a String.
You should first remove the commas and then parse it to a number.

var i = 0;
var array_nums = ["3,000", "2,000", "1,000"];
for (i = 0; i < array_nums.length; i++) {
  array_nums[i] = parseInt(array_nums[i].replace(new RegExp(",", 'g'), "")) * 1000;
}
console.log(array_nums);
Thusitha
  • 3,393
  • 3
  • 21
  • 33
0

You can do it in a forEach and use toLocaleString() to format with commas. Straight Javascript ... no JQuery necessary.

var array_output=[];
var array_nums = [3000, 2000, 1000];

array_nums.forEach(x => {
  array_output.push((x*1000).toLocaleString());
})
jbrown
  • 3,025
  • 1
  • 15
  • 22
0

if you are using ES6 try let newArr = array_nums.map(data => data*1000)

LowCool
  • 1,187
  • 5
  • 25
  • 51