0

I have array of objects. objects have "Poured, Sold, Loss and Variance" properties as string. I want to convert string to float. I need to persist that comma separator and double point precision. Here is the example array of objects.

[{"ID":"September-2016", "Product":"September-2016", "Poured":"111,759.07", "Sold":"107,660.97", "Loss":"-4,098.10", "Variance":"-3.67", "startDate":"2016-09-01", "endDate":"2016-09-22"}, {"ID":"November-2015", "Product":"November-2015", "Poured":"53,690.25", "Sold":"52,953.60", "Loss":"-736.65", "Variance":"-1.37", "startDate":"2015-11-20", "endDate":"2015-11-30"}, {"ID":"May-2016", "Product":"May-2016", "Poured":"156,401.65", "Sold":"151,192.51", "Loss":"-5,209.14", "Variance":"-3.33", "startDate":"2016-05-03", "endDate":"2016-05-31"}, {"ID":"March-2016", "Product":"March-2016", "Poured":"49,260.22", "Sold":"49,399.14", "Loss":"138.92", "Variance":"0.28", "startDate":"2016-03-01", "endDate":"2016-03-09"}, {"ID":"June-2016", "Product":"June-2016", "Poured":"162,126.88", "Sold":"161,718.62", "Loss":"-408.26", "Variance":"-0.25", "startDate":"2016-06-01", "endDate":"2016-06-30"}, {"ID":"July-2016", "Product":"July-2016", "Poured":"160,185.68", "Sold":"154,882.40", "Loss":"-5,303.28", "Variance":"-3.31", "startDate":"2016-07-01", "endDate":"2016-07-31"}, {"ID":"January-2016", "Product":"January-2016", "Poured":"355,509.26", "Sold":"179,696.72", "Loss":"-175,812.54", "Variance":"-49.45", "startDate":"2016-01-01", "endDate":"2016-01-31"}, {"ID":"February-2016", "Product":"February-2016", "Poured":"150,980.73", "Sold":"146,248.72", "Loss":"-4,732.01", "Variance":"-3.13", "startDate":"2016-02-01", "endDate":"2016-02-29"}, {"ID":"December-2015", "Product":"December-2015", "Poured":"167,843.42", "Sold":"163,732.95", "Loss":"-4,110.47", "Variance":"-2.45", "startDate":"2015-12-01", "endDate":"2015-12-31"}, {"ID":"August-2016", "Product":"August-2016", "Poured":"168,853.51", "Sold":"160,024.84", "Loss":"-8,828.67", "Variance":"-5.23", "startDate":"2016-08-01", "endDate":"2016-08-31"}]

I need like

[{"ID":"September-2016", "Product":"September-2016", "Poured":111,759.07, "Sold":107,660.97, "Loss":-4,098.10, "Variance":-3.67, "startDate":"2016-09-01", "endDate":"2016-09-22"}, {"ID":"November-2015", "Product":"November-2015", "Poured":53,690.25, "Sold":52,953.60, "Loss":-736.65, "Variance":-1.37, "startDate":"2015-11-20", "endDate":"2015-11-30"}, {"ID":"May-2016", "Product":"May-2016", "Poured":156,401.65, "Sold":151,192.51, "Loss":-5,209.14, "Variance":-3.33, "startDate":"2016-05-03", "endDate":"2016-05-31"}, {"ID":"March-2016", "Product":"March-2016", "Poured":49,260.22, "Sold":49,399.14, "Loss":138.92, "Variance":0.28, "startDate":"2016-03-01", "endDate":"2016-03-09"}, {"ID":"June-2016", "Product":"June-2016", "Poured":162,126.88, "Sold":161,718.62, "Loss":-408.26, "Variance":-0.25, "startDate":"2016-06-01", "endDate":"2016-06-30"}, {"ID":"July-2016", "Product":"July-2016", "Poured":160,185.68, "Sold":154,882.40, "Loss":-5,303.28, "Variance":-3.31, "startDate":"2016-07-01", "endDate":"2016-07-31"}, {"ID":"January-2016", "Product":"January-2016", "Poured":355,509.26, "Sold":179,696.72, "Loss":-175,812.54, "Variance":-49.45, "startDate":"2016-01-01", "endDate":"2016-01-31"}, {"ID":"February-2016", "Product":"February-2016", "Poured":150,980.73, "Sold":146,248.72, "Loss":-4,732.01, "Variance":-3.13, "startDate":"2016-02-01", "endDate":"2016-02-29"}, {"ID":"December-2015", "Product":"December-2015", "Poured":167,843.42, "Sold":163,732.95, "Loss":-4,110.47, "Variance":-2.45, "startDate":2015-12-01, "endDate":"2015-12-31"}, {"ID":"August-2016", "Product":"August-2016", "Poured":168,853.51, "Sold":160,024.84, "Loss":-8,828.67, "Variance":-5.23, "startDate":"2016-08-01", "endDate":"2016-08-31"}]

Please let me know how can I achieve that.

Sorry I mentioned it as "String to Integer" initially that should be "String to Float"

19
  • 5
    You CANNOT have a number with comma separators and specific precision in JS. There is a single numeric type that JavaScript knows and it doesn't format it. Commented Sep 23, 2016 at 19:05
  • 1
    If you want to preserve the precision, multiply all the numbers by 100 and round to the nearest integer, and use that instead of the fractions. Commented Sep 23, 2016 at 19:07
  • You CAN produce the desired output however - but it will be a string, not a number, nor would it be easily convertible to number in JS. Commented Sep 23, 2016 at 19:07
  • @e-neko why would it not be easily convertable to number? Essentially all you do is remove thousand separators, replace the fraction separator (if altered) then pass it through Number. Commented Sep 23, 2016 at 19:09
  • I almost called it a duplicate... But this one includes the misconception of keeping formatting and precision rules in a number. Commented Sep 23, 2016 at 19:09

4 Answers 4

1

Since you want a float, then i assume that it need to be programmable usable and don't even need to be human readable - cuz the only way you will get that is by keeping it as a string...

var data = [{"ID":"September-2016", "Product":"September-2016", "Poured":"111,759.07", "Sold":"107,660.97", "Loss":"-4,098.10", "Variance":"-3.67", "startDate":"2016-09-01", "endDate":"2016-09-22"}, {"ID":"November-2015", "Product":"November-2015", "Poured":"53,690.25", "Sold":"52,953.60", "Loss":"-736.65", "Variance":"-1.37", "startDate":"2015-11-20", "endDate":"2015-11-30"}, {"ID":"May-2016", "Product":"May-2016", "Poured":"156,401.65", "Sold":"151,192.51", "Loss":"-5,209.14", "Variance":"-3.33", "startDate":"2016-05-03", "endDate":"2016-05-31"}, {"ID":"March-2016", "Product":"March-2016", "Poured":"49,260.22", "Sold":"49,399.14", "Loss":"138.92", "Variance":"0.28", "startDate":"2016-03-01", "endDate":"2016-03-09"}, {"ID":"June-2016", "Product":"June-2016", "Poured":"162,126.88", "Sold":"161,718.62", "Loss":"-408.26", "Variance":"-0.25", "startDate":"2016-06-01", "endDate":"2016-06-30"}, {"ID":"July-2016", "Product":"July-2016", "Poured":"160,185.68", "Sold":"154,882.40", "Loss":"-5,303.28", "Variance":"-3.31", "startDate":"2016-07-01", "endDate":"2016-07-31"}, {"ID":"January-2016", "Product":"January-2016", "Poured":"355,509.26", "Sold":"179,696.72", "Loss":"-175,812.54", "Variance":"-49.45", "startDate":"2016-01-01", "endDate":"2016-01-31"}, {"ID":"February-2016", "Product":"February-2016", "Poured":"150,980.73", "Sold":"146,248.72", "Loss":"-4,732.01", "Variance":"-3.13", "startDate":"2016-02-01", "endDate":"2016-02-29"}, {"ID":"December-2015", "Product":"December-2015", "Poured":"167,843.42", "Sold":"163,732.95", "Loss":"-4,110.47", "Variance":"-2.45", "startDate":"2015-12-01", "endDate":"2015-12-31"}, {"ID":"August-2016", "Product":"August-2016", "Poured":"168,853.51", "Sold":"160,024.84", "Loss":"-8,828.67", "Variance":"-5.23", "startDate":"2016-08-01", "endDate":"2016-08-31"}]

data.forEach(function(item){
  item.Poured = parseFloat(item.Poured.replace(/,/g, ''))
  // item.Sold = parseFloat(item.Sold.replace(/,/g, ''))
  // item.Variance = parseFloat(item.Variance.replace(/,/g, ''))
})

console.log(data)

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much for the reply. In my mind I fixed need to show values with comma separator and double point precision and then while user clicks header should be sort. So changing string to float(comma separator) achieving the both requirement. Now I got clear idea to add one more field for sorting purpose and use existing field for show on page. You are very awesome. For this I tried long time today
0

You could use a sequence of operations:

  1. replace "," with ""
  2. parseFloat()
  3. toFixed(2)

4 Comments

The third step will give you a string.
But that creates a string, not an integer, as the OP incorrectly imagines is possible.
Yes, but if the value is to have two decimals, it needs to be a string.
Then you should point out that what the OP is asking for is impossible and/or meaningless.
0

what you are expecting is wrong & you can get the result as an array of numbers or coma separated string, please refer below code and output.

var RESULT = YOUR_RESPOSE.map(function(a){
['Poured','Sold', 'Variance', 'Loss'].forEach(function(v){
 if(typeof a[v] === "string"){
    // if you want as an array
    a[v] = a[v].split(',').map(function(s){return isNaN(s) ? s : (+s).toFixed(2)});
    // if you want as an string
    //a[v] = a[v].split(',').map(function(s){return isNaN(s) ? s : (+s).toFixed(2)}).join(',');
 }
});

return a;

})



"[{"ID":"September-2016","Product":"September-2016","Poured":["111.00","759.07"],"Sold":["107.00","660.97"],"Loss":["-4.00","98.10"],"Variance":["-3.67"],"startDate":"2016-09-01","endDate":"2016-09-22"},{"ID":"November-2015","Product":"November-2015","Poured":["53.00","690.25"],"Sold":["52.00","953.60"],"Loss":["-736.65"],"Variance":["-1.37"],"startDate":"2015-11-20","endDate":"2015-11-30"},{"ID":"May-2016","Product":"May-2016","Poured":["156.00","401.65"],"Sold":["151.00","192.51"],"Loss":["-5.00","209.14"],"Variance":["-3.33"],"startDate":"2016-05-03","endDate":"2016-05-31"},{"ID":"March-2016","Product":"March-2016","Poured":["49.00","260.22"],"Sold":["49.00","399.14"],"Loss":["138.92"],"Variance":["0.28"],"startDate":"2016-03-01","endDate":"2016-03-09"},{"ID":"June-2016","Product":"June-2016","Poured":["162.00","126.88"],"Sold":["161.00","718.62"],"Loss":["-408.26"],"Variance":["-0.25"],"startDate":"2016-06-01","endDate":"2016-06-30"},{"ID":"July-2016","Product":"July-2016","Poured":["160.00","185.68"],"Sold":["154.00","882.40"],"Loss":["-5.00","303.28"],"Variance":["-3.31"],"startDate":"2016-07-01","endDate":"2016-07-31"},{"ID":"January-2016","Product":"January-2016","Poured":["355.00","509.26"],"Sold":["179.00","696.72"],"Loss":["-175.00","812.54"],"Variance":["-49.45"],"startDate":"2016-01-01","endDate":"2016-01-31"},{"ID":"February-2016","Product":"February-2016","Poured":["150.00","980.73"],"Sold":["146.00","248.72"],"Loss":["-4.00","732.01"],"Variance":["-3.13"],"startDate":"2016-02-01","endDate":"2016-02-29"},{"ID":"December-2015","Product":"December-2015","Poured":["167.00","843.42"],"Sold":["163.00","732.95"],"Loss":["-4.00","110.47"],"Variance":["-2.45"],"startDate":"2015-12-01","endDate":"2015-12-31"},{"ID":"August-2016","Product":"August-2016","Poured":["168.00","853.51"],"Sold":["160.00","24.84"],"Loss":["-8.00","828.67"],"Variance":["-5.23"],"startDate":"2016-08-01","endDate":"2016-08-31"}]"

2 Comments

.split(',').map I don't think that's what you want to do - you are converting each chunk of the comma separated number.
@Manivannan Sorry I couldn't get what you are trying to tell from your answer. Can you please explain bit more
0

You use the term "integers", but I assume you mean "numbers" (not "floats", since that concept does not exist in JS)--since integers have no decimal part by definition.

Numbers in JavaScript do not and cannot have thousands separators, nor specifiable precision--in other words, 1.00 is exactly equivalent to 1. Therefore, you cannot convert a string to some non-existent form of integer with separators and precision.

If you're so inclined, check out the section of the ES spec about numeric literals.

Thousands separators and/or some fixed precision are aspects of the representation of a number, taking the form of a string.

1 Comment

mistakenly typed string to integer. My mind set to get "123, 567.75" to 123,456.75. Thank you so much for your reference

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.