Forum Moderators: coopster

Message Too Old, No Replies

take a string and insert a character in it

take a string and insert a character in it

         

drooh

7:30 am on Oct 31, 2007 (gmt 0)

10+ Year Member



I need to take a number and insert a period in it, also the number may be one, two, three or more figures

example

$num = 1
then I want to make it .1

also
$num = 23
then i want to make it 2.3

also
$num = 413
then i want to make it 41.3

is there a php function that can do something like this?

Habtom

9:06 am on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



is there a php function that can do something like this?

I am not sure if there is one PHP function which can do that but you can try something like the following:

$num = "423467";
$last_num = substr($num,-1);
$line = rtrim($num, $last_num).".".$last_num;

if you wish you can put this in a function:

function add_period($num) {
$last_num = substr($num,-1);
$line = rtrim($num, $last_num).".".$last_num;
return $line;
}

to be used later in the following format:

echo add_period($input_num);

Habtom

[edited by: Habtom at 9:15 am (utc) on Oct. 31, 2007]