ProLuck
ProLuck

Reputation: 375

How to add value substring inside decimal in PHP

I have a number like this

"0.321527778"

I want make some substring from my value in top and then for the substring result I want input that number into the my decimal number, how can I do? (for substring I cut from 2 last digits from decimal number)

for the expetation result will be like this :

"0.321527777777778"

code :

<?php

  $number = "0.321527778";

  $substring = substr($number, -2, -1);

  print_r($number . ' ' . $substring);

?>

result :

0.321527778 7

If my explanation is incomprehensible, I apologize, and you can ask me again, Thank You

Upvotes: 0

Views: 96

Answers (1)

Barmar
Barmar

Reputation: 780871

You need to get the substrings before and after the part that you want to insert multiple times. Then concatenate everything without putting spaces between them.

$number = "0.321527778";
$beginning = substr($number, 0, -2);
$end = substr($number, -1);
$substring = substr($number, -2, -1);
$result = $beginning . str_repeat($substring, 6) . $end;
echo $result;

Upvotes: 1

Related Questions