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