-1

I have been handling long numbers in PHP. Like the following examples.

12.020000
12.000000

To get rid of trailing zeros and the decimal point I have been using the following inside a function.

return rtrim(rtrim($str, "0"),".");

So the above turns out like.

12.02
12

It was a bit short sighted as when 1000 gets entered it gets turned into 1.

Can someone please help me with the code to remove trailing zeros after the decimal point only?

Bonus points if the code removes the decimal place but I can always feed it into rtim($str,".").

EDIT: To be clear, I am stripping the decimal place and zeros only when displaying to the screen. Also casting to float is not an option as I also handle numbers like 0.00000001 which come out like 1.0e-9 sort of thing.

Peter Bushnell
  • 918
  • 1
  • 13
  • 30
  • 5
    Why not use number_format() or sprintf(), rather than messing about with rtrim() – Mark Baker Oct 10 '13 at 12:12
  • possible duplicate of [How to strip trailing zeros in PHP](http://stackoverflow.com/questions/5149129/how-to-strip-trailing-zeros-in-php) – Glavić Oct 10 '13 at 12:13
  • "If string does not contain `.` add it at end before feeding into your expression" - so `1000` becomes `1000.` and the dot "protects" the zeros. – Floris Oct 10 '13 at 12:15
  • `(float) $str` and that's all – Sal00m Oct 10 '13 at 12:18
  • Casting to floats does not look good with numbers like 0.00000001 which I am handling. It makes then unreadable to general users. – Peter Bushnell Oct 10 '13 at 13:13

2 Answers2

5

Why are you using string to hold numbers? Cast it to float and it'll solve your problem.

$string = '12.020000';
$number = (float) $string; // will be 12.02

Then, if you want to use it as string (but why?)

$string = (string) $number;
Elon Than
  • 9,603
  • 4
  • 27
  • 37
3

The thing that perplexes me about your question is that extra zeros won't be included in a number variable without intentionally adding them with number_format. (This may be why someone down-voted it).

Normally you don't want to use string functions (meant for text) on variables that hold numbers. If you want to round off a number, use a function like round.

http://php.net/manual/en/function.round.php

There's also number_format, which can format numbers by adding zero padding: (it doesn't actuall round, just trims off excess numbers).

http://php.net/manual/en/function.number-format.php

Since your zeros are appearing, it's likely that you simply need to multiple the variable by 1, which will essentially convert a string to a number.

Good luck!

Jason Silver
  • 527
  • 7
  • 23
  • 1
    You should use `(float)` or `floatval()` to cast to float, don't use other workarounds. – Elon Than Oct 10 '13 at 12:22
  • Internally the system uses the full numbers. I am only trimming numbers when displaying to the screen. Casting to float is no good as I need to also display number like 0.00000001 which come out in an unfriendly way to users with float. – Peter Bushnell Oct 10 '13 at 13:16