Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adnanerlansyah403's avatar

How to convert string to number format and round it the value.

Hello everyone, I have a problem how to convert string to number or money format. I'm already used method number_format and money_format from php but it still not work. The callback of error's the value must be a float. So what must I do for this? So for example :

$total_price = "134.400"
// After convert it, the result must be like this 
$total_price = 134.400

And thank you so much for all 🙏

0 likes
13 replies
undeportedmexican's avatar

I'm not sure I understand your question. If it's a matter of having the number stored as text ($number = '21'), the only thing you need to do is add (int) or (float) before the variable to cast it as such:

$number_as_string = '21';

$number = (float) $number_as_string;

There may be a more elegant way to handle this, but I think this could work.

muhdbasiir's avatar

Send your code here for us to see. So that we all can see and may be able to fix it.

adnanerlansyah403's avatar

@muhdbasiir@gmail.com

I just want the result value to be like this :

$total_price = "134.400"
// After convert it the result is like this.
$total_price = 134.400 
cg0012's avatar
$string_value = "3.567";

$number_value = (float)$string_value;
//3.567

$round_sig_digits = round($number_value, 1);
//3.6

$round_up = ceil($number_value);
//4.0

$round_down = floor($number_value);
//3.0

$round_whole = intval($round_up);
//4
1 like
adnanerlansyah403's avatar

@cg0012 how about make / convert string to type data int for manage calculation Because if I'm using a float or int, it's will be redirect a int without decimal or value zero.

So I want to be like this :

$total_price = "134.400"
// After convert it the result is like this.
$total_price = 134.400 
cg0012's avatar

can you post your calculations? I don't understand. You can use the string for calculations:

$total_price = "134.400";

$double_price = 2 * $total_price;
//268.8

$price_formatted = number_format($double_price, 2);
//"268.80"
1 like
adnanerlansyah403's avatar

@cg0012 I already tried it with

$total_price = "134.400";

$double_price = 2 * $total_price;
//268.8

But its get an error, like this

A non-numeric value encountered

adnanerlansyah403's avatar

@cg0012

this is my code :

and for coupon_percent_off, i have a 40%

				$cartTotal = Cart::total();  // For example, this value is : "100000"
                
                if($coupon_amount == 0 || $coupon_amount == null) {
                    return response()->json([
                        'message' => 'Coupon amount has run out!',
                    ]);
                } else if ($coupon_minimum_order_amount > $cartTotal) {
                    return response()->json([
                        'message' => 'Minimum order amount is $'.$coupon_minimum_order_amount.'!',
                    ]);
                } else {
                    Session::put( 'coupon_applied' ,[
                        'coupon_name' => $coupon_check->coupon_name,
                         'coupon_percent_off' => $coupon_check->coupon_percent_off,
                         'discount_amount' => $cartTotal * $coupon_check->coupon_percent_off / 100,
                         'total_amount' => $cartTotal - ($cartTotal * $coupon_check->coupon_percent_off / 100),
                     ]);
     
                     return response()->json([
                         'message' => 'Coupon applied successfully!',
                     ]);
                }
cg0012's avatar

@adnanerlansyah403

try it with your calcs outside of your response array:

$cartTotal = Cart::total();  // For example, this value is : "100000"

if($coupon_amount == 0 || $coupon_amount == null) {
	return response()->json([
		 'message' => 'Coupon amount has run out!',
	]);
} 
elseif($coupon_minimum_order_amount > $cartTotal) {
	return response()->json([
		'message' => 'Minimum order amount is $'.$coupon_minimum_order_amount.'!',
	]);
} 
else {

	$discountAmount = $cartTotal * $coupon_check->coupon_percent_off / 100;
	$totalAmount = $cartTotal - ($cartTotal * $coupon_check->coupon_percent_off / 100);

	Session::put( 'coupon_applied' ,[
		'coupon_name' => $coupon_check->coupon_name,
		'coupon_percent_off' => $coupon_check->coupon_percent_off,
		'discount_amount' => $discountAmount,
		'total_amount' => $totalAmount,
	]);
     
	return response()->json([
		'message' => 'Coupon applied successfully!',
	]);
}
1 like
adnanerlansyah403's avatar

@cg0012

I still keep getting this error :

"message": "A non-numeric value encountered",
"exception": "ErrorException",
cg0012's avatar

@adnanerlansyah403 start dumping your variables at each step until you find where its failing.

$cartTotal = Cart::total();  // For example, this value is : "100000"

dd($cartTotal);

//rest of code
}

//if that looks good then ->

$cartTotal = Cart::total();  // For example, this value is : "100000"

dd($coupon_amount);

//rest of code
}

//next try

$cartTotal = Cart::total();  // For example, this value is : "100000"

if($coupon_amount == 0 || $coupon_amount == null) {
	return response()->json([
		 'message' => 'Coupon amount has run out!',
	]);
} 

dd($coupon_minimum_order_amount);

}

//you could also debug by using if statements

if(is_numeric($coupon_check->coupon_percent_off)) {
		dd("percent off is numeric")
}
else {
		dd('hey the percent off is jacked up");
}
1 like
Sinnbeck's avatar

You cannot have trailing zeroes on a float. It will automatically be trimmed to 134.4. If you need the zeroes, use a string

$total_price = "134.400"
$total_price = (float) $total_price;
134.4
1 like
jlrdw's avatar

@adnanerlansyah403 number_format was already mentioned above my suggestion would be to go to the the php manual and read up on it, it's pretty powerful.

1 like

Please or to participate in this conversation.