Ruby浮点数后没有任何零

时间:2021-07-22 16:50:56

I had searched a lot to get my exact requirement for getting the float value without unwanted zero after decimal.

我已经搜索了很多,以获得浮点值的确切要求,而不需要在十进制数之后为零。

Eg:  14.0 should be 14
     14.1 should be 14.1

Nearest possible solution I found so far is using sprintf():

我目前找到的最接近的解决方案是使用sprintf():

irb(main):050:0> num = 123.0
=> 123.0
irb(main):051:0> sprintf('%g', num)
=> "123"

Problem here is my num type changed to String from Float. Can I get the float value change without its type changed?

这里的问题是我的num类型从Float变为String。我可以在不改变类型的情况下更改浮点值吗?

7 个解决方案

#1


5  

14.0.tap{|x| break x.to_i == x ? x.to_i : x}
# => 14

14.1.tap{|x| break x.to_i == x ? x.to_i : x}
# => 14.1

#2


5  

Try:

试一试:

class Float
  def try_integer
    to_i == self ? to_i : self
  end
end

14.2.try_integer    #=> 14.2
14.0.try_integer    #=> 14

#3


2  

Well I got my answer through Sawa's and BroiSatse's answers.

我通过萨瓦和布洛瓦萨茨的回答得到了答案。

But I guess following is enough to get what I required:

但我想,以下这些就足够满足我的要求了:

irb(main):057:0> num = 14.0
=> 14.0
irb(main):058:0> num = num == num.to_i ? num.to_i : num
=> 14
irb(main):059:0> num = 14.1
=> 14.1
irb(main):060:0> num = num == num.to_i ? num.to_i : num
=> 14.1

#4


1  

Would you be asking for the Integer part of the float value?

你会要求浮点值的整数部分吗?

Integer part of 123.0 is 123 and of 156.78 is 156.

123.0的整数部分是123,156.78的整数部分是156。

If so, It'd be:

如果是这样的话,这将是:

2.1.0 :001 > 123.0.to_i
 => 123
2.1.0 :002 > 156.7.to_i
 => 156

#5


1  

I would suggest something like

我想提出类似的建议

class Float
  def custom_format(num)
    num.round(0) == num ? num : num.round(1)
  end
end

13.1.custom_format #=> 13.1
13.7.custom_format #=> 13.7
13.0.custom_format #=> 13

#6


1  

I would like to add a method to Numeric parent class so that this method can also be used with Integer(Fixnum) numbers. Using == to do comparison as it does not do typecasting before comparing.

我想向数字父类添加一个方法,这样这个方法也可以用于整数(Fixnum)数字。使用==进行比较,因为在进行比较之前不进行类型转换。

class Numeric
  def slim(places = nil)
    truncate == self ? truncate : places.nil? ? self : round(places)
  end
end

#7


0  

Assuming that you want to remove the zeros, only if it contains purely zeros and otherwise return the original value, I would do

假设您要删除0,仅当它包含纯零并返回原始值时,我才会这样做

num = 123.00
(num.to_s.scan(/[.]\d+/)[0].to_f > 0) ? num : num.to_i #=> 123

num = 123.45
(num.to_s.scan(/[.]\d+/)[0].to_f > 0) ? num : num.to_i #=> 123.45

#1


5  

14.0.tap{|x| break x.to_i == x ? x.to_i : x}
# => 14

14.1.tap{|x| break x.to_i == x ? x.to_i : x}
# => 14.1

#2


5  

Try:

试一试:

class Float
  def try_integer
    to_i == self ? to_i : self
  end
end

14.2.try_integer    #=> 14.2
14.0.try_integer    #=> 14

#3


2  

Well I got my answer through Sawa's and BroiSatse's answers.

我通过萨瓦和布洛瓦萨茨的回答得到了答案。

But I guess following is enough to get what I required:

但我想,以下这些就足够满足我的要求了:

irb(main):057:0> num = 14.0
=> 14.0
irb(main):058:0> num = num == num.to_i ? num.to_i : num
=> 14
irb(main):059:0> num = 14.1
=> 14.1
irb(main):060:0> num = num == num.to_i ? num.to_i : num
=> 14.1

#4


1  

Would you be asking for the Integer part of the float value?

你会要求浮点值的整数部分吗?

Integer part of 123.0 is 123 and of 156.78 is 156.

123.0的整数部分是123,156.78的整数部分是156。

If so, It'd be:

如果是这样的话,这将是:

2.1.0 :001 > 123.0.to_i
 => 123
2.1.0 :002 > 156.7.to_i
 => 156

#5


1  

I would suggest something like

我想提出类似的建议

class Float
  def custom_format(num)
    num.round(0) == num ? num : num.round(1)
  end
end

13.1.custom_format #=> 13.1
13.7.custom_format #=> 13.7
13.0.custom_format #=> 13

#6


1  

I would like to add a method to Numeric parent class so that this method can also be used with Integer(Fixnum) numbers. Using == to do comparison as it does not do typecasting before comparing.

我想向数字父类添加一个方法,这样这个方法也可以用于整数(Fixnum)数字。使用==进行比较,因为在进行比较之前不进行类型转换。

class Numeric
  def slim(places = nil)
    truncate == self ? truncate : places.nil? ? self : round(places)
  end
end

#7


0  

Assuming that you want to remove the zeros, only if it contains purely zeros and otherwise return the original value, I would do

假设您要删除0,仅当它包含纯零并返回原始值时,我才会这样做

num = 123.00
(num.to_s.scan(/[.]\d+/)[0].to_f > 0) ? num : num.to_i #=> 123

num = 123.45
(num.to_s.scan(/[.]\d+/)[0].to_f > 0) ? num : num.to_i #=> 123.45