【问题标题】:I want to change this format, '$1,000,000', to '1000000' [duplicate]我想将这种格式“$ 1,000,000”更改为“1000000”[重复]
【发布时间】:2012-10-16 17:44:45
【问题描述】:

可能重复: How to print a number with commas as thousands separators in JavaScript

我正在尝试以这种格式获取值,$1,000,000。现在我得到了这种格式的值,1000000,它工作正常,但我不想要这个。我希望它的价值为 1,000,000 美元,并在我的 PHP 代码中更改并接受它。

我的HTML:

<form action="index.php" method="Get">
    Enter the present value of pet: <input type="text" name="v" value="1000000"/><br>
    Enter the value of the pet you want: <input type="text" name="sv" value="1951153458"/><br>

    <input type="submit" />
</form>

这是我的 PHP:

<?php
    $i           = 0;
    $v           = isset($_GET['v']) ? (float) $_GET['v'] : 1000000;
    $sv          = isset($_GET['sv']) ? (float) $_GET['sv'] : 1951153458;
    $petearn     = 0;
    $firstowner  = 0;
    $secondowner = 0;

    And so on..............

我的计算器以这种方式工作正常:

http://ffsng.deewayz.in/index.php?v=1000000&sv=1951153458

但我希望它是:

http://ffsng.deewayz.in/index.php?v=$1,000,000&sv=$1,951,153,458

我很困惑如何将这种格式 $1,000,000 更改为 1000000 这个 或者如果有其他方法。我需要使用任何 JavaScript 代码吗?在提交表单之前?

有人试图通过以下方式帮助我,但我不知道如何使用它。

function reverse_number_format($num)
{
    $num = (float)str_replace(array(',', '$'), '', $num);
}

【问题讨论】:

标签: php javascript html


【解决方案1】:

只需替换字符串中的任何非数字字符:

$filteredValue = preg_replace('/[^0-9]/', '', $value);

UPD:

$value = '$1,951,1fd53,4.43.34'; // User submitted value

// Replace any non-numerical characters but leave dots
$filteredValue = preg_replace('/[^0-9.]+/', '', $value);

// Retrieve "dollars" and "cents" (if exists) parts
preg_match('/^(?<dollars>.*?)(\.(?<cents>[0-9]+))?$/', $filteredValue, $matches);

// Combine dollars and cents
$resultValue = 0;
if (isset($matches['dollars'])) {
    $resultValue = str_replace('.', '', $matches['dollars']);
    if (isset($matches['cents'])) {
        $resultValue .= '.' . $matches['cents'];
    }
}

echo $resultValue; // Result: 1951153443.34

【讨论】:

  • +1,但如果用户输入小数点和分会怎样?
  • 用户必须输入:$1,000,000 等
  • 这可能是您希望他们输入的内容,但如果他们实际输入 $1,234,567.89 并且您使用此答案处理它,您最终会得到 123456789,这显然不是输入的意思.你需要处理它——优秀程序员的关键技能之一是能够预测无效输入并安全地处理它。
  • @SDC 是的,谢谢,我也需要弄清楚 :)
  • 如果我没看错,123.123.123 仍然会失败,只是一个边缘情况。 :)
【解决方案2】:
$num = preg_replace('/[\$,]/', '', $num);

【讨论】:

  • +1,但是如果用户输入空格而不是(或同样)逗号会发生什么?或者其他你没有想到的非数字字符?
  • @SDC,没有什么比增加更多的要求,听起来应该是一个错误并且无效或者它只是一个不同的reg exp。
【解决方案3】:

使用您提供的功能来做到这一点:

    $v = 1000000;
if(isset($_GET['v'])){
  $v = reverse_number_format($_GET['v']);
}

在你的 reverse_number_format 函数中添加行 return $num;

【讨论】:

    【解决方案4】:

    你应该使用 PHP 的 floatval 函数。

    【讨论】:

      【解决方案5】:

      在服务器上进行计算,就像您已经在做的那样。然后只需使用掩码将其显示给用户。

      喜欢:

      function formated(nStr) {
          curr = '$ ';
          nStr += '';
          x = nStr.split('.');
          x1 = x[0];
          x2 = x.length > 1 ? '.' + x[1] : '';
          var rgx = /(\d+)(\d{3})/;
          while (rgx.test(x1)) {
              x1 = x1.replace(rgx, '$1' + ',' + '$2');
          }
          if (x1 + x2) {
              return curr + x1 + x2
          }
          else {
              return ''
          }
      }
      

      在http://jsfiddle.net/RASG/RXWTM/ 上查看工作示例。

      【讨论】:

      • 但是在提交表单时服务器仍然需要删除这些值!发帖人希望用户输入值。它与显示值无关。
      • @epascarello:Stack Overflow 经常发生这种情况。用户是否知道他可以戴上面具,所以他不必剥离任何东西?有时答案很简单,只是问题让它看起来很复杂。
      • 只是说这只是问题的一半,因为它必须再次在服务器级别进行过滤。
      • @epascarello:看看他的 html:value="1000000"。他没有任何奇怪的符号。只有数字。无需过滤任何东西。
      • 但他希望用户以这种方式输入并在服务器上处理它,阅读问题。
      猜你喜欢
      • 2012-09-06
      • 2017-01-14
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      • 2021-09-23
      相关资源
      最近更新 更多