0

How to convert this 19,500 string to number. If I do this

<?php

$val = "19,500";
$number = explode("," , $val);
$newnumber = $number[0].$number[1];

?>

But I don't think this is correct way.

2
  • Are you only trying to remove commas? When you say you want to convert to a number, what specifically do you mean? An integer? A float? A numeric string? Commented Jan 24, 2014 at 4:49
  • You want to remove just , or you want to convert to integer? Commented Jan 24, 2014 at 5:11

8 Answers 8

2

LIVE DEMO

You can replace the string ',' to '' and then convert into integer by int

<?php

$number = "19,500";
$updatedNumber = str_replace(',','',$number);

echo (int)$updatedNumber ;

NOTE: int is better to use than intval function. It reduces overhead of calling the function in terms of Speed. http://objectmix.com/php/493962-intval-vs-int.html

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

<?php
$val = "19,500";
$val = str_replace(',', '.', $val);
$number = (float)$val;
?>

UPDATED: if comma comes out as a thousands-separator then:

<?php
$val = "19,500";
$val = str_replace(',', '', $val);
$number = (int)$val;
?>

1 Comment

This depends very much on whether , is used as a decimal point or a thousands-separator. In the US, your result would be off by a factor of a thousand -- even more if there were two commas.
0

you can simply replace ',' with ''

$newnumber = str_replace(',', '', $val);

Comments

0

the best way is:

<?php
  $val = "19,500";
  $tmp = str_replace(',', '', $val);
  $newnumber = (float)$tmp;
?>

Comments

0

I think you just want to remove the ,:

<?php
$val = "19,500";
$val = strtr($val, array(',' => ''));
$number = intval($val);
var_dump($number);

use strtr() instead of str_replace() in case of multiple ,

Comments

0

When I see your code it's just sting string(5) "19500"

<?php
 $val = "19,500";
 $number = explode("," , $val);
 $newnumber = $number[0].$number[1];
 var_dump($newnumber);
?>

so you can convert to integer like the following

<?php
    $val = "19,500";
    $number = explode("," , $val);
    $newnumber = $number[0].$number[1];
    $realinteger = (int)($newnumber);
    var_dump($realinteger);
?>

So the result will be int(19500)

1 Comment

What about 19,500,200
0

Just try this code

$val = "19,500";
$newnumber = intval(str_replace(',', '', str_replace('.', '', $val)));  // output : 19500

$val = "19,500.25";
$newnumber = intval(str_replace(',', '', str_replace('.', '', $val)));  // output : 1950025

you can edit delimiter that want to replace with balnk string :)

Or you can try this

$newnumber = preg_match("/[^0-9,. -]/", $val) ? 0 : preg_replace("/[^0-9.-]/", "",$val);

5 Comments

You almost never want to remove both commas and periods, unless what you have isn't really a number. Doing so throws off the numeric value by several orders of magnitude.
@cHao => Question mentioned that "how to convert string to number?" then if string contain alpha numeric value, it will automatically convert to 0.
No, it'll automatically stop parsing at the first character that doesn't belong. So intval('19,500') would be equal to 19. But for integers, you want the decimal point (and everything after it) ignored, not interpreted as more digits. So, intval('19500.25') would be equal to 19500, which is almost certainly the desired result. Your example outputs a number a hundred times the string's numeric value.
@chao => Yes, you are right !!! And solution is : $newnumber = preg_match("/[^0-9,. -]/", $val) ? 0 : preg_replace("/[^0-9.-]/", "",$val);
That'd work...mostly...though it differs from the built-in behavior in that any wacky character causes the result to be 0. I'm of two minds about whether that's a good thing. Depends on whether that degree of strictness is called for, i guess.
0

just try one. simple as that :)

<?php
$val = "19,500";
$val = str_replace(',','', $val);
$newnumber = number_format($val,2);

?>

OUTPUT: 19,500.00

  • If you want to have no decimal you can change the 2 in 0. Like this.

OUTPUT: 19,500

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.