简体   繁体   English

将字符串解析为浮点数

[英]Parse string to floating point number

I'm struggling trying to parse some string that I'm reading from a txt file.我正在努力解析我从 txt 文件中读取的一些字符串。 When I parse it using parseFloat() it doesn't work as expected, for example when I log a "45.64" parsed to float it shows: 45.63999938964844.当我使用 parseFloat() 解析它时,它无法按预期工作,例如,当我记录一个解析为浮动的“45.64”时,它显示:45.63999938964844。 This is my code:这是我的代码:

  const linesProd = productosTxt.split(/\r?\n/);
  var productosObject = { "Productos": [] };
  linesProd.forEach(line => {
    const fields = line.split("|");
    
    if (fields[0]){
      console.log(fields[0])
      console.log(typeof(fields[5]))
      var fieldsObject = {
        productId: fields[0],
        id: fields[1],
        name: fields[2],
        cost: Number.parseFloat(fields[3]),
        price1: Number.parseFloat(fields[4]),
        price2: Number.parseFloat(fields[5]),
      };
      
      productosObject = { ...productosObject, "Productos": [...productosObject.Productos, fieldsObject] };
    }
  });

Those fields (cost, price1, price2) are suposed to save those fields from my txt file in number format.这些字段(成本,价格1,价格2)应该以数字格式保存我的txt文件中的这些字段。 The lines in the txt file look something like this: txt 文件中的行如下所示:

A1|1|name1|50.5|70|80

A2|2|name2|25.25|35.6|90.15

I already took a look at some posts here but can't find a proper answer我已经在这里查看了一些帖子,但找不到正确的答案

Any help would be appreciated, Thanks任何帮助将不胜感激,谢谢

Edit1: Log of the fields in the array (changed some variable names but they are likely the same so you can see) Edit1:数组中字段的日志(更改了一些变量名称,但它们可能相同,因此您可以看到)

在此处输入图像描述

Use +yourString instead改用+yourString

  const linesProd = productosTxt.split(/\r?\n/);
  var productosObject = { "Productos": [] };
  linesProd.forEach(line => {
    const fields = line.split("|");
    
    if (fields[0]){
      console.log(fields[0])
      console.log(typeof(fields[5]))
      var fieldsObject = {
        productId: fields[0],
        id: fields[1],
        name: fields[2],
        cost: +fields[3],
        price1: +fields[4],
        price2: +fields[5],
      };
      
      productosObject = { ...productosObject, "Productos": [...productosObject.Productos, fieldsObject] };
    }
  });

Try this:尝试这个:

const linesProd = productosTxt.split(/\r?\n/);
var productosObject = { Productos: [] };
linesProd.forEach((line) => {
  const fields = line.split("|");

  if (fields[0]) {
    console.log(fields[0]);
    console.log(typeof fields[5]);
    var fieldsObject = {
      productId: fields[0],
      id: fields[1],
      name: fields[2],
      cost: Number.parseFloat(fields[3]).toFixed(2),
      price1: Number.parseFloat(fields[4]).toFixed(2),
      price2: Number.parseFloat(fields[5]).toFixed(2),
    };

    productosObject = { ...productosObject, Productos: [...productosObject.Productos, fieldsObject] };
  }
});

Fix the parsing to two decimal places, that should give you the number you expect.将解析修复到小数点后两位,这应该会给你你期望的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM