-1

重複の可能性: 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);
}
4

5 に答える 5

4

文字列から数字以外の文字を置き換えるだけです。

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

更新日:

$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
于 2012-10-26T12:55:45.153 に答える
3
$num = preg_replace('/[\$,]/', '', $num);
于 2012-10-26T12:55:12.983 に答える
1

提供した関数を使用してそれを行うには:

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

reverse_number_format 関数に行を追加しますreturn $num;

于 2012-10-26T12:58:16.727 に答える
0

すでに行っているように、サーバー上で計算を行います。次に、マスクを使用してユーザーに表示します。

お気に入り:

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/で実際のサンプルを参照してください。

于 2012-10-26T13:01:06.833 に答える
0

PHP の floatval関数を使用する必要があります。

于 2012-10-26T12:54:29.010 に答える