خرید بک لینک
Is there a way to prevent behavior of dropping zeros when using array.map(Number) method ?
Example1: converts the string x9310.50x94 to 10.5
Example2: converts the string x9329.10x94 to 29.1

To complete a coding challenge, I must submit an array with elements that are numbers with two (2) decimal points, the elements can't be strings. Acceptable example: 57.76 unacceptable example:x9457.76x94.
I have used toFixed(2) to trim down numbers with excess decimal numbers for instance 57.7645 to 57.76. Trouble is the toFixed method converts the number to a string.

I then used array.map(Number) to cast the elements back to numbers.
Problem is using the array.map(Number) method results in dropping zeros.
For instance x9310.50x94 becomes 10.5 and 10.5 is unacceptable for this coding challenge.

Is there a way to make array.map(Number) return without dropping the zeros? Or is there a better way entirely to accomplish what I am trying to do here?
I am pretty sure math.round can't help me here.,

Here is my script so far:

Code:

<script type="text/javascript">
                        var bills = [50.23, 19.12, 34.01,100.11, 12.15, 9.90, 29.11, 12.99,10.00, 99.22, 102.20, 100.10, 6.77, 2.22];
                        console.log(bills);  // these are logged as numbers
                                               
                        var billsTips = bills.map(function(num) {
                                var num = num + num * .15;
                                return  num.toFixed(2);
                        });
                       
                        console.log(billsTips);  // these are logged as strings
                       
                        var totals= billsTips.map(Number);
                        console.log(totals);            // these are logged as numbers with zeros dropped
                        console.log(typeof(totals[8])); // no longer a string, is as type number
                        console.log(totals[8]);  // 11.5 should be 11.50
       
                </script>

Thanks in advance for looking at this.

برچسب: نویسنده: آیلین رضوانی تاريخ: دوشنبه 2 بهمن 1396 ساعت: 18:47

صفحه بندی