Float Arithmetic: Return A Fixnum When Float == Fixnum

j = 5/2.0

=> 2.5

I'm fine with this

j = 4/2.0

=> 2.0

Useless precision -for me. I want 2!

j == j.to_i ? j.to_i : j

=> 2

Is there a "better" way?

MaggotChild wrote:

j = 5/2.0

=> 2.5

I'm fine with this

j = 4/2.0

=> 2.0

Useless precision -for me. I want 2!

The notation is misleading. It's not a question of precision, but of
type. You can think of 2.0 as 2:Float, not 2 to 2 significant figures.

j == j.to_i ? j.to_i : j

=> 2

Is there a "better" way?

Why even bother?

Best,

···

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

Marnen Laibow-Koser wrote:

MaggotChild wrote:

j = 5/2.0

=> 2.5

I'm fine with this

j = 4/2.0

=> 2.0

Useless precision -for me. I want 2!

The notation is misleading. It's not a question of precision, but of
type. You can think of 2.0 as 2:Float, not 2 to 2 significant figures.

j == j.to_i ? j.to_i : j

=> 2

Is there a "better" way?

Why even bother?

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org

It occurs to me that you may also want to play with Rational.

Best,

···

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

MaggotChild wrote:
>>> j = 5/2.0
> => 2.5

> I'm fine with this

>>> j = 4/2.0
> => 2.0

> Useless precision -for me. I want 2!

The notation is misleading. It's not a question of precision, but of
type. You can think of 2.0 as 2:Float, not 2 to 2 significant figures.

Yes, I do, but when it comes to printing, Float does not.

>>> j == j.to_i ? j.to_i : j
> => 2

> Is there a "better" way?

Why even bother?

OCD

···

On Dec 1, 5:52 am, Marnen Laibow-Koser <mar...@marnen.org> wrote:

MaggotChild wrote:

The notation is misleading. �It's not a question of precision, but of
type. �You can think of 2.0 as 2:Float, not 2 to 2 significant figures.

Yes, I do, but when it comes to printing, Float does not.

You can always redefine Float.to_s. Changing the underlying type,
though, is probably a poor idea.

>>> j == j.to_i ? j.to_i : j
> => 2

> Is there a "better" way?

Why even bother?

OCD

Apply your OCD in the right places, though! :slight_smile:

Best,

···

On Dec 1, 5:52�am, Marnen Laibow-Koser <mar...@marnen.org> wrote:

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

Marnen Laibow-Koser wrote:

MaggotChild wrote:

The notation is misleading. �It's not a question of precision, but of
type. �You can think of 2.0 as 2:Float, not 2 to 2 significant figures.

Yes, I do, but when it comes to printing, Float does not.

You can always redefine Float.to_s. Changing the underlying type, though, is probably a poor idea.

j == j.to_i ? j.to_i : j

=> 2
Is there a "better" way?

Why even bother?

OCD

Apply your OCD in the right places, though! :slight_smile:

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org

Ugly, but what the hey:

j = 2.0
sprintf('%g', j) --> "2"

j = 2.50000001
sprintf('%g', j) --> "2.5"

Works on integers, too:

j = 2
sprintf('%g', j) --> "2"

Glen

···

On Dec 1, 5:52�am, Marnen Laibow-Koser <mar...@marnen.org> wrote:

Ah, I knew it -a better way. And to think, it was with a printf
format... All those years of C down the toilet

···

On Dec 1, 12:24 pm, "Glen F. Pankow" <Glen.F.Pan...@noaa.gov> wrote:

MaggotChild wrote:

j == j.to_i ? j.to_i : j
=> 2
Is there a "better" way?

Ugly, but what the hey:

j = 2.0
sprintf('%g', j) --> "2"

j = 2.50000001
sprintf('%g', j) --> "2.5"

Works on integers, too:

j = 2
sprintf('%g', j) --> "2"