Joppo
Joppo

Reputation: 729

how to get value before and behind decimal point in string

My PHP file receives (via $_POST) strings (with constant prefix) like:

(constant prefix in this example = 'astring'; figure before and after the decimal point can vary in size)

How to get the value behind the decimal point? I know how to extract the total figure from the constant prefix, but I need to use the figures before and after the decimal point and don't know how to extract those. Using preg_match in some way?

Upvotes: 3

Views: 13137

Answers (6)

Dinesh
Dinesh

Reputation: 343

If you want the number before the decimal. Try This.

$number = 2.33;
echo floor($number);

Upvotes: 5

Altaf Hussain
Altaf Hussain

Reputation: 1048

If you want only numbers, use like this

$str = 'astring23.2'; 
$str_arr = explode('.',$str);
echo preg_replace("/[a-z]/i","",$str_arr[0]);  // Before the Decimal point
echo $str_arr[1]; // After decimal point

Upvotes: 0

Krish R
Krish R

Reputation: 22711

Do you want something similar?

    <?php
        $string = "astring23.6";
        $data = explode("astring", $string);    // removed prefix string "astring" and get the decimal value            
        list($BeforeDot, $afterDot)=explode(".", $data[1]); //split the decimal value   
        echo "BeforeDot:".$BeforeDot." afterDot: ".  $afterDot;

    ?>

Upvotes: 0

Benno
Benno

Reputation: 3008

A simple way (php 5.3+):

$str = 'astring23.2';
$pre = strstr($str, '.', true);

Upvotes: 3

user399666
user399666

Reputation: 19879

list($before, $after) = explode(".", $string);

echo "$before is the value before the decimal point!";
echo "$after is the value after the decimal point!";

Upvotes: 3

GautamD31
GautamD31

Reputation: 28763

Try with explode like

$str = 'astring23.2'; 
$str_arr = explode('.',$str);
echo $str_arr[0];  // Before the Decimal point
echo $str_arr[1];  // After the Decimal point

Upvotes: 3

Related Questions