-2

I am building an app with Ionic and Firebase. I am having numbers with decimals sometimes and I would like to truncate them as integer.

Here is my code :

var visitMaster = [0, 0, 0, 0, 0, 0, 0];

        $timeout(function(){

          var visitRef = firebase.database().ref('/business/' + myId + '/visits/');

          // LAST HOUR
          visitRef.orderByChild("visit").startAt(nowMinusOne).endAt(now).once("value", function(snapshot) {
          var visitOneHour = (snapshot.numChildren() / 2);
          if (visitOneHour === ""){
          visitMaster.splice(6, 1, 0);
        } else {
          visitMaster.splice(6, 1, visitOneHour);
        }
          });

How to render the numbers in my visitMaster as int ?

UPDATE : each value of the array is supposed to go in a chart (bar) and so I display it that way :

//WE PUT IT IN THE CHART:
        $scope.labels = ["H-6", "H-5", "H-4", "H-3", "H-2", "H-1", "Now"];
        $scope.data = [
          visitMaster,

        ];

I would like that each value be an integer (most of them are x.5)

FrenchyNYC
  • 337
  • 1
  • 6
  • 23

2 Answers2

1

use the function Math.floor() to truncate a floating point number to its integer part. And use Array.prototype.map to process the entire array,.

visitMaster = [1, 1.5, 2.1, 2.8, 111.345];
visitMaster = visitMaster.map(Math.floor);
console.log(visitMaster);
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

If you're trying to sum the value of visitMaster into a single integer value, you can do the following:

For ES5.

var total = visitMaster.reduce(function (total, visit) {
    return total + visit;
}, 0);

Or if using ES6.

let total = visitMaster.reduce((total, visit) => total + visit, 0);

And then to convert to an integer, I'd suggest using Math.ceil, which will round the value up to the next whole number.

total = Math.ceil(total);

Update To just convert each value in your array to an integer, just use the Array.prototype.map function. Thanks to @Barmar for correction.

visitMaster = visitMaster.map(function (value) {
    return Math.floor(value); // or Math.ceil(value);
});
fubar
  • 16,918
  • 4
  • 37
  • 43
  • "sometimes and I would like to truncate them as integer." that's `parseInt`, not `Math.ciel` – Cory Danielson May 05 '17 at 00:46
  • Hello, I added an update to my question, maybe it will be clearer ? Thank you in advance – FrenchyNYC May 05 '17 at 00:46
  • @CoryDanielson I was suggesting `Math.ceil` over `parseInt`, because it looks like the values are visit durations. So where `parseInt` will round down, this wouldn't necessary show the time required for a visit, whereas rounding up using `Math.ceil` would. – fubar May 05 '17 at 00:49
  • 1
    `Math.floor()` is the proper function for rounding down. `parseInt()` is for parsing strings containing integers. It will work for this because it first converts the number to a string and then parses it, but `Math.floor()` is simpler because it doesn't have to do any conversion. – Barmar May 05 '17 at 00:58