0

I'm just trying to see my total amount of number variables with decimals but it's not working. My platform is Oracle Apex 20.2

var model = apex.region("itemig").widget().interactiveGrid("getViews", "grid").model;
console.log(model);
var amtKey = model.getFieldKey("ORAN");
//console.log(amtKey);
var totAmt = 0;
model.forEach(function(r) {
    var n_amount = parseInt(r[amtKey], 10);

    totAmt += n_amount;
    console.log(n_amount);
});

    $s('P705_TOTALORAN',totAmt);


Output was:

function Number() { [native code] }100507580901124105

What I want is to get the amount of numbers in totAmt with decimals. I'm not getting far because of this native code. Can someone tell me what it is and hopefully, more importantly I can complete this part in my work?

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
OnurSari
  • 33
  • 10
  • `totAmt = Number` and `n_amount = Number` just assign the global `Number` function to the two variables. It doesn't make them into numbers. What is that supposed to actually do, as it's just wrong. Was it supposed to be `0` instead? – VLAZ Nov 12 '20 at 07:56
  • It was var totAmt = 0; I just changed it and didn't work neither. – OnurSari Nov 12 '20 at 07:57
  • Also you have `var n_amount` twice in the code – mplungjan Nov 12 '20 at 07:57
  • what do you expect to get if you assign `Number`? i dont know maybe you want to defined types? then use typescript – bill.gates Nov 12 '20 at 07:58
  • You can use reduce instead of that forEach – mplungjan Nov 12 '20 at 08:02
  • Guys code is working only problem that there is no showing decimals total amount.That's why I'm trying to figure out it but not working. – OnurSari Nov 12 '20 at 08:12
  • If you want the decimals, don't use `parseInt()` as it literally parses an integer, i.e. strips away the decimals. Use `parseFloat()` instead. – Lennholm Nov 12 '20 at 08:23
  • or just `+r[amtKey]` – mplungjan Nov 12 '20 at 08:24
  • If it's returned NaN ? – OnurSari Nov 12 '20 at 08:27
  • Absolutely this should be more clear to understand and developers. When try it with toFixed method it worked but not completely. `parseFloat` or `+r[amtKey]` it returned NaN as Not a Number – OnurSari Nov 12 '20 at 08:29

2 Answers2

2

Javascript is not a typed language. The type of a variable is deduced using its value.

If you want to declare the variables totAmt and n_amount to be of type number, you should use var/let and assign a number to them:

var totAmt = 0;           // these two variables contain numbers..
var n_amount = 0;         // .. so they are of type "number"

Otherwise, if you do var totAmt = Number;, you are just assigning the global object Number which is a native constructor for number objects, and when you add that using the + operator to another variable, the toString of that constructor will be called and that yields the string "function Number() { [native code] }" which is then concatenated to your other numbers:

console.log(Number.toString());

console.log(Number + 5);

Note 1: If you want totAmt to be displayed with 2 decimal points then use toFixed before you display it like so:

$s("P705_TOTALORAN", totAmt.toFixed(2));

Note 2: If the values r[amtKey] are decimal numbers, then parseInt will only get the whole-number part of those decimals. You should use parseFloat, Number or the unary + operator to parse the decimal part as well. Also, the forEach can be replaced by a reduce to shorten the code like so:

var totAmt = model.reduce(function(sum, r) {
  return sum + Number(r[amtKey]);               // Number will keep the decimal part of the number, whereas parseInt will only get the whole-number part
}, 0);

$s('P705_TOTALORAN', totAmt.toFixed(2));
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
1

What var totAmt = Number; is doing is setting totAmt to the literal function Number, it does not tell JS that totAmt is supposed to be a number.

If you then add another number to it, it uses the totAmt.toString() method, which just returns function Number() { [native code] } and concatenates the number n_amount as a String to it.

When you remove the duplicate variable declaration, this code would work:

var totAmt = 0;
model.forEach(function(r) {
    var n_amount = parseInt(r[amtKey], 10);

    totAmt += n_amount;
    console.log(n_amount);
});

Also if you are adding numbers in an array up, JS has the Array#reduce() method for that, it makes your code much more concise:

let totAmt = model.reduce((acc,cV)=>acc+cV[amtKey],0);
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44