-1

Possible Duplicate: How to print a number with commas as thousands separators in JavaScript

I am trying to get the value in this format, $1,000,000. Right now I am getting the value in this format, 1000000, and it's working fine, but I don't want this. I want it to get the value as $1,000,000 and change it in my PHP code and accept it.

My 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>

And here is my 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..............

My calculator is working fine in this way:

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

But I want it to be:

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

I'm confused anout how to change this format $1,000,000 to 1000000 this or if there is other way. Do I need to use any JavaScript code? Before the form is submitted?

Someone tried to help me the following way, but I have no clue on how to use it.

function reverse_number_format($num)
{
    $num = (float)str_replace(array(',', '$'), '', $num);
}
Community
  • 1
  • 1
deerox
  • 1,045
  • 3
  • 14
  • 28

5 Answers5

4

Just replace any non-numerical characters from the string:

$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
Vadim Ashikhman
  • 9,851
  • 1
  • 35
  • 39
  • 1
    +1, but what happens if the user enters a decimal point and cents? – SDC Oct 26 '12 at 12:58
  • user has to enter : $1,000,000 etc – deerox Oct 26 '12 at 13:00
  • 3
    that may be what you want them to enter, but if they actually enter $1,234,567.89 and you process it using this answer, you'll end up with 123456789, which is clearly not what was meant by the input. You need to handle that -- One of the key skills of a good programmer is the ability to anticipate invalid input and deal with it safely. – SDC Oct 26 '12 at 13:05
  • @SDC yes thanks i need to figure that out too :) – deerox Oct 26 '12 at 13:08
  • :) that is better and well explained bro thanks :) – deerox Oct 26 '12 at 13:36
  • 1
    If I am reading it right, it would still fail for 123.123.123, just an edge case. :) – epascarello Oct 26 '12 at 13:45
  • I corrected the retrieving parts piece of code a bit. The user can now submit any number of numbers to the "cents" part. Here: (?[0-9]{1,2}))? to (?[0-9]+))? – Vadim Ashikhman Oct 26 '12 at 13:46
  • 123.123.123 => 123123.123 What behavior do you expect? Nevermind, i noticed you posted the comment just before i fixed this issue :) – Vadim Ashikhman Oct 26 '12 at 13:49
3
$num = preg_replace('/[\$,]/', '', $num);
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • 1
    +1, but what happens if the user enters spaces instead of (or as well as) the commas? Or some other non-numeric character that you haven't thought of? – SDC Oct 26 '12 at 12:59
  • @SDC, nothing like adding more requirements, sounds like that should be an error and not valid or it is just a different reg exp. – epascarello Oct 26 '12 at 13:01
1

To do it using the function you provided:

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

in your reverse_number_format function add the line return $num;

Kev Price
  • 799
  • 9
  • 21
0

You should use PHPs floatval function.

undefined
  • 2,051
  • 3
  • 26
  • 47
0

Do your calculation on the server, like you're already doing. Then just use a mask to show it to the user.

Like:

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 ''
    }
}

See a working sample at http://jsfiddle.net/RASG/RXWTM/.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RASG
  • 5,988
  • 4
  • 26
  • 47
  • BUT the server would still need to strip the values when the form is submitted! The poster wants the user to enter in the value. It has nothing to do with displaying the value. – epascarello Oct 26 '12 at 13:05
  • @epascarello: it happens a lot here at [so]. does the user **know** he can apply a mask, so he doesnt have to strip anything? sometimes the answer is simple, just the question make it look complicated. – RASG Oct 26 '12 at 13:07
  • Just saying it is only HALF the question since it has to be filtered at the server level once again. – epascarello Oct 26 '12 at 13:26
  • @epascarello: look at his html: `value="1000000"`. he doesnt have any strange symbols. only numbers. no need to filter anything. – RASG Oct 26 '12 at 13:31
  • BUT He wants the user to enter it that way and handle it on the server, read the question. – epascarello Oct 26 '12 at 13:44