Formatting and casting numbers from strings

Hello, I’m having problems understanding how to format and cast (ie “type”) numbers from strings.

I have an XML object that is returning strings, and I want to format some as number strings and others to perform some simple maths on.

The PHP manual gives this example:
[php]<?php

$number = 1234.56;

// english notation (default)
$english_format_number = number_format($number);
// 1,235

?>[/php]

So I tried:

[php]print("
Sales rank: " . number_format($current->SalesRank));[/php]

But this returns no number where I would expect something formatted like: 123,456,789.

So I conclude here that I first have to cast the string as an integer or float, the documentation suggests:

[php]<?php
$foo = “5bar”; // string
$bar = true; // boolean

settype($foo, “integer”); // $foo is now 5 (integer)
settype($bar, “string”); // $bar is now “1” (string)
?>[/php]

So I assume something like this:

[php]print("
Sales rank: " . number_format(settype($current->SalesRank, “integer”)));[/php]

But, again, where I expect a formatted number all I ever get is “1”, ie “Sales rank: 1”, regardless of the input string.

I additionally want to do some maths on similarly acquired strings, but I’ll leave that for another post.

Thanks

What is the string you’re dealing with? If I’ve understood correctly I’d say that you’re using a string like “1,234” and are trying to deal with it as an integer - typecasting that into a number will stop at the first non-numeric character (the comma), making the output just 1. To avoid this, just run:

[php]$number_str = “1,234,567”;
$number = (int)str_replace(’,’,’’,$number_str);[/php]

You also need to tell it how many decimal places to use

$number = (int)str_replace(’,’,’’,$number_str);
$english_format_number = number_format($number, 2);

Right now the string is just a straight integer, there are no non-numeric characters. So, one value could be “22”, another value could be “7654321”. They are all integer values, with no formatting, and I don’t need any decimal places (my understanding of the documentation was that the default format was “english” with no decimal places).

I’d also prefer to output the formatted string in one go, ie like my code sample:

[php]print("
Sales rank: " . number_format(settype($current->SalesRank, “integer”)));[/php]

Rather than create another variable and then work with that.

These work:

[php]$rankint = (int)$current->SalesRank;

print("
Sales rank: " . number_format($rankint));
//output: “Sales rank: 1,482”

print("
Sales rank: " . $current->SalesRank);
//output: “Sales rank: 1482”
[/php]

But, how to do that in one line, ie without the need to set another variable first, ie just manipulate the XML variable object in place?

Thanks

number_format()

Try that?

If its already an integer, why are you setting the type? just take that out and use number_format. the only thing you’ll have to manually do is the $ sign.

Ok, I’m not being clear: the value is a string (but it takes the visual form of an integer: there are no decimal points and there are no formatting characters).

This string may look like any one of these:

“2”, “32”, 1024", “23584”, “7654321”

I want to take this string and format it like:

“2”, “32”, “1,024”, “23,584”, “7,654,321”

I also want to understand how to cast strings to integers or floats so that I can do maths functions on them (elsewhere).

So far, I haven’t really got very far, because it seems a bit pointless to have to create a variable first, before being able to convert it to an integer in order to format it back as a (more readable) string.

These are the results:

[php]
$rankint = (int)$current->SalesRank;

print("
Sales rank: " . number_format($rankint));
//outputs “Sales rank: 1,024”

print("
Sales rank: " . number_format($current->SalesRank));
//outputs "Sales rank: "

print("
Sales rank: " . $current->SalesRank);
//outputs “Sales rank: 1024”

[/php]

So, the first example has the right output, but requires the formation of a (second) variable first. How do I take the string that is the product of “$current->SalesRank” and format it as required?

ie, something like:

[php]
print("
Sales rank: " . number_format(settype($current->SalesRank, “integer”)));
[/php]

Or, is that really too difficult to do in PHP?

How about:

[php]number_format(intval($current->SalesRank));[/php]

http://uk.php.net/intval

http://uk.php.net/floatval

Thanks, that’s the one, solved!

Did a bit more testing/thinking, and found another method that works too:

[php]
$rankint = (int)$current->SalesRank; //input “1234”

print("
Sales rank: " . number_format($rankint));
//output: “Sales rank: 1,234”

print("
Sales rank: " . number_format(intval($current->SalesRank)));
//output: “Sales rank: 1,234”

print("
Sales rank: " . number_format((int)($current->SalesRank)));
//output: “Sales rank: 1,234”

print("
Sales rank: " . number_format((float)($current->SalesRank)));
//output: “Sales rank: 1,234”

print("
Sales rank: " . $current->SalesRank);
//output: “Sales rank: 1234”

[/php]

All your doing is telling the script what kind of number to use. It just depends on what your trying to accomplish.

I think that’s the point, telling the script that it is a number (to cast is as a number type), so that you can do number functions on it, ie use “number_format”, because, as demonstrated, you can’t use “number_format” on a string.

Isn’t the purpose clear enough from what I’ve already written?

Sponsor our Newsletter | Privacy Policy | Terms of Service