将字符串转换为数字:创建错误的数字

huangapple go评论61阅读模式
英文:

How to convert String in number : create wrong number

问题

在我的表格中,我有一个价格,它可能是字符串或数字,但我尝试将字符串转换为数字,但不起作用。

起初,我的表格中的数字是这样的(一行是名称,一行是价格):

Apple iPhone 13 128 Go Minuit	|  1 379€00
Apple iPhone 14 128 Go Minuit	|  1 379€00
Apple iPhone 14 128 Go Lumière Stellaire	|  919€00
Apple iPhone 14 Pro 128 Go Noir Sidéral	|  919€00

重构后,我有了以下内容,并且您可以看到它是数字(num)还是字符串:

Apple iPhone 13 128 Go Minuit	|  1 379.00
Apple iPhone 14 128 Go Minuit	|  1 379.00
Apple iPhone 14 128 Go Lumière Stellaire	|  919.00 num
Apple iPhone 14 Pro 128 Go Noir Sidéral	  |  919.00 num

如果我尝试应用以下代码:

if (is_string($price_format)) {
  $price_format = number_format((float)$price_format, 2, '.', '');
}

结果是:

Apple iPhone 13 128 Go Minuit	|  1.00 num
Apple iPhone 14 128 Go Minuit	|  1.00 num
Apple iPhone 14 128 Go Lumière Stellaire |  919.00 num

如您所见,我得到了1.00,而不是价格,但一切都是数字。我需要的结果是像1 379.00这样的价格数字,而不是字符串。

以下是我的方法:

foreach ($result as $item) {
  $price = $item['1'];

  $convert_array = ['$', '€', '£', '¥', '₩', '₹', '₽', '฿', '₺', '₴'];

  foreach ($convert_array as $symbol) {
    if (strpos($price, $symbol) !== false) {
      $price = str_replace($symbol, '.', $price);
      $price = preg_replace('/\. /', '', $price, 1);
      $price = str_replace(',', '.', $price);
      break;
    }
  }

  $price_format = trim($price);

  if (substr($price_format, -1) == '.') {
    $price_format = substr($price_format, 0, -1);
  }

  if (is_numeric($price_format)) {
    $price_format = $price_format . ' num';
  }
  print_r($price_format);
}

查看结果图

英文:

In my table I have a price could be string or number but I tried to convert the string in number but it does work.

At the origin the number is like that inside my table (on row for the name and one row for the price)

Apple iPhone 13 128 Go Minuit	|  1 379€00
Apple iPhone 14 128 Go Minuit	|  1 379€00
Apple iPhone 14 128 Go Lumière Stellaire	|  919€00
Apple iPhone 14 Pro 128 Go Noir Sidéral	|  919€00

After refactoring I have this and you can see if it's a numeric (num) or a string

Apple iPhone 13 128 Go Minuit	|  1 379.00
Apple iPhone 14 128 Go Minuit	|  1 379.00
Apple iPhone 14 128 Go Lumière Stellaire	|  919.00 num
Apple iPhone 14 Pro 128 Go Noir Sidéral	  |  919.00 num

If I tried to applied that :

I also tried with floatval but does not work

if (is_string($price_format)) {
  $price_format = number_format((float)$price_format, 2, '.', '');
}

The result is :

Apple iPhone 13 128 Go Minuit	|  1.00 num
Apple iPhone 14 128 Go Minuit	|  1.00 num
Apple iPhone 14 128 Go Lumière Stellaire |  919.00 num

As you see I have 1.00 and not the price but everything is numeric

the result I need is a price number like that 1 379.00 and not a string

Below my approach

foreach ($result as $item) {
  $price = $item['1'];

  $convert_array = ['$', '€', '£', '¥', '₩', '₹', '₽', '฿', '₺', '₴'];

  foreach ($convert_array as $symbol) {
    if (strpos($price, $symbol) !== false) {
      $price = str_replace($symbol, '.', $price);
      $price = preg_replace('/\. /', '', $price, 1);
      $price = str_replace(',', '.', $price);
      break;
    }
  }

  $price_format = trim($price);

  if (substr($price_format, -1) == '.') {
    $price_format = substr($price_format, 0, -1);
  }

/*
  if (is_string($price_format)) {
    $price_format = number_format((float)$price_format, 2, '.', '');
  }
*/
  if (is_numeric($price_format)) {
    $price_format = $price_format . ' num';
  }
  print_r($price_format);
}

Image result

答案1

得分: 1

如果您期望的千位分隔符是空格,您可以尝试先移除空格(float 转换后空格后面的内容会被忽略),然后再使用 number_format() 的最后一个参数 ' ' 来添加空格:

if (is_string($price_format)) {
    $price_format = number_format((float)str_replace(' ', '', $price_format), 2, '.', ' ');
}

在这个示例中(https://3v4l.org/OCSWN),结果是:

1 379.00
1 379.00
919.00 num
919.00 num
英文:

If your expected thousand separator for the input number is space you can try, removing the space first (anything after the space is ignored by the float conversion) and then adding it again using ' ' as the last parameter of number_format():

if (is_string($price_format)) {
    $price_format = number_format((float)str_replace(' ','',$price_format), 2, '.', ' ');
  }

In the example here (https://3v4l.org/OCSWN) the result is:

1 379.00
1 379.00
919.00 num
919.00 num

huangapple
  • 本文由 发表于 2023年4月4日 04:13:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定