Javascript:string to Float

时间:2016-09-23 19:04:23

标签: javascript

我有一些对象。对象具有“倾倒,出售,损失和方差”属性作为字符串。我想将字符串转换为float。我需要坚持逗号分隔符和双点精度。这是对象的示例数组。

[{"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"}]

我需要像

[{"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"}]

请让我知道如何实现这一目标。

抱歉,我最初提到它为“ String to Integer ”,应该是“ String to Float

4 个答案:

答案 0 :(得分:1)

既然你想要一个浮点数,那么我认为它需要是可编程的可用的,甚至不需要是人类可读的 - 因为你得到它的唯一方法是将它保持为字符串...

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)

答案 1 :(得分:0)

您可以使用一系列操作:

  1. 将“,”替换为“”
  2. parseFloat()
  3. toFixed(2)

答案 2 :(得分:0)

你使用术语“integers”,但我认为你的意思是“数字”(不是“浮动”,因为JS中不存在这个概念) - 因为整数根据定义没有小数部分。

JavaScript中的数字不具有千位分隔符,也不具有可指定的精度 - 换句话说,#main-banner .bg-animation{ background: url('http://pentathemes.com/saasapp/images/intro-bg.gif') 0 0 repeat; -webkit-animation: animate_background 10s linear 0s infinite; -moz-animation: animate_background 10s linear 0s infinite; -o-animation: animate_background 10s linear 0s infinite; animation: animate_background 10s linear 0s infinite; height:100vh; } @-webkit-keyframes animate_background { from { background-position: 0 0; } to { background-position: 0 -200px; } } @-moz-keyframes animate_background { from { background-position: 0 0; } to { background-position: 0 -200px; } } @-o-keyframes animate_background { from { background-position: 0 0; } to { background-position: 0 -200px; } } @keyframes animate_background { from { background-position: 0 0; } to { background-position: 0 -200px; } } 完全等同于1.00。因此,您无法使用分隔符和精度将字符串转换为某种不存在的整数形式。

如果您愿意,请查看ES规范中有关numeric literals的部分。

数千个分隔符和/或某些固定精度是数字的表示的一部分,采用字符串的形式。

答案 3 :(得分:0)

你所期待的是错误的&您可以将结果作为数字或逗号分隔的字符串数组得到,请参考下面的代码和输出。

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"}]"