0

Say I have 1.234 I want to get the .234 I tried echo 1.234%1 //I get 0
I am rusty, what is the correct way?

(The tags says PHP as this might be an issue only with PHP, but I really am looking for the general solution).

Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
  • 1
    `$x = $x - floor($x)` from http://stackoverflow.com/questions/50801/whats-the-best-way-to-get-the-fractional-part-of-a-float-in-php – mowwwalker Aug 20 '11 at 22:44
  • The answer to this question is contained within https://stackoverflow.com/q/6619377/2943403 – mickmackusa Oct 21 '21 at 02:44
  • @mickmackusa see the answer I accepted vs the answers in the thread u linked to. – Itay Moav -Malimovka Oct 22 '21 at 03:33
  • Stack Overflow is flooded with redundant content. I am trying to relate what has not yet been related by the system. `fmod()` is demonstrated here: https://stackoverflow.com/a/33562331/2943403 – mickmackusa Oct 22 '21 at 03:38
  • I see it in another way. Software changes, I expect to see same questions over and over, with new answers all the time. It is admirable what u try to do. But I would use this effort to create something rather than cleaning – Itay Moav -Malimovka Oct 22 '21 at 16:02

5 Answers5

3

php's % modulo operator converts its arguments to integers. To get a floating-point modulus, use fmod:

echo fmod(1.234, 1);
phihag
  • 278,196
  • 72
  • 453
  • 469
1

You can remove the whole number from the number itself. in php its:

$num = 1.234;
echo $num - floor($num);
Daniel
  • 30,896
  • 18
  • 85
  • 139
0

Subtract the integer portion of $x ((int)$x) from $x:

$x = 1.234;
$d = $x - (int)$x;
// $d now equals 0.234
echo $d;

Example

Paul
  • 139,544
  • 27
  • 275
  • 264
0

Just substract integer part 1.234 - (int)1.234

Mchl
  • 61,444
  • 9
  • 118
  • 120
0

Try this:

echo 1.234 - intval(1.234);

elerium115
  • 21
  • 3