Search code examples
sumparsefloat

get the sum/combined values using parseFloast


i have been trying to figure out how to get the sum of the 3 inventory totals using parseFloat

"extract1" field would represent the sum of already extracted data from the "In_Stock", "Stock_national" and "Stock_24H" fields, which should = 14 (0+3+11) but instead is showing 113 as a result of the combined values.

how should i write this in order for it to show 14 as total? this is a final step for me in be able to complete an automated inventory update process and would greatly appreciate any help!

parseFloat(In_Stock+Stock_national+Stock_24H)

and shows this in json format

{
"ID":[
{
"name":"185/65R15 88T BFG ADVANTAGE T/A SPORT",
"sku":"98559",
"Product_Number":"98559.1",
"In_Stock":"0",
"Stock_24H":"3",
"Stock_national":"11",
"selection2":[
{
"name":"0"
},
{
"name":"3"
},
{
"name":"11"
}
],
"extract1":113
}

Solution

  • Figured it out, need to convert each variable to a number by calling parseFloat() around each one and it shows total addition of numbers.

    parseFloat(In_Stock) + parseFloat(Stock_24H) + parseFloat(Stock_national)

    resulsts note: (extract1 replaced with id_stock_total)

    {
    "ID":[
    {
    "name":"185/65R15 88T BFG ADVANTAGE T/A SPORT",
    "sku":"98559",
    "Product_Number":"98559.1",
    "In_Stock":"0",
    "Stock_24H":"3",
    "Stock_national":"11",
    "id_stock_total":14
    },