While working on a project, I encountered an issue with assigning float values fetched from a REST API using Angular. Instead of correctly assigning the float value, it was being set as 0.
To illustrate my problem clearly, let's consider the following scenario:
$scope.commodities = {
items: [],
total: 0,
total_gst: 0
};
for (var i = 0; i < $scope.output.data.length; i++) {
var itms = {};
itms.item_description = $scope.output.data[i].item_description;
itms.currency = $scope.output.data[i].currency;
itms.amount = parseFloat($scope.output.data[i].amount);
itms.ex_rate = parseFloat($scope.output.data[i].ex_rate);
itms.tax = parseFloat($scope.output.data[i].tax);
itms.quantity = parseFloat($scope.output.data[i].quantity);
itms.total_without_gst = parseFloat($scope.output.data[i].total_without_gst);
itms.gst = parseFloat($scope.output.data[i].gst);
itms.total_with_gst = parseFloat($scope.output.data[i].total_with_gst);
console.log(itms.total_without_gst);
console.log(itms);
$scope.commodities.items.push(itms);
}
Here is the data received from the REST API:
{
"status": true,
"data": [
{
"item_description": "Ocean freight",
"currency": "USD",
"tax": 0,
"amount": 150,
"ex_rate": 4.42,
"quantity": 1,
"total_without_gst": 663,
"gst": 0,
"total_with_gst": 663
},
{
"item_description": "DG charges",
"currency": "USD",
"tax": 0,
"amount": 80,
"ex_rate": 4.42,
"quantity": 1,
"total_without_gst": 353.6,
"gst": 0,
"total_with_gst": 353.6
}
]
}
A screenshot of the console log can be viewed here.