如果浮点组件不是.00 sprintf/printf,则只显示小数点

时间:2022-12-28 17:07:50

I am pretty formatting a floating point number but want it to appear as an integer if there is no relevant floating point number.

我很喜欢格式化一个浮点数,但是如果没有相关的浮点数,我希望它显示为一个整数。

I.e.

即。

  • 1.20 -> 1.2x
  • 1.20 - > 1.2 x
  • 1.78 -> 1.78x
  • 1.78 - > 1.78 x
  • 0.80 -> 0.8x
  • 0.80 - > 0.8 x
  • 2.00 -> 2x
  • 2.00 - > 2 x

I can achieve this with a bit of regex but wondering if there is a sprintf-only way of doing this?

我可以使用一些regex来实现这一点,但是我想知道是否有一种只使用sprintf的方式来实现这一点?

I am doing it rather lazily in ruby like so:

我用ruby很懒地这样做:

("%0.2fx" % (factor / 100.0)).gsub(/\.?0+x$/,'x')

8 个解决方案

#1


45  

You want to use %g instead of %f:

你想用%g而不是%f:

"%gx" % (factor / 100.00)

#2


26  

You can mix and match %g and %f like so:

可以这样混合和匹配%g和%f:

"%g" % ("%.2f" % number)

#3


15  

If you're using rails, you can use rails' NumberHelper methods: http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html

如果使用rails,可以使用rails的NumberHelper方法:http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html

number_with_precision(13.001, precision: 2, strip_insignificant_zeros: true)
# => 13
number_with_precision(13.005, precision: 2, strip_insignificant_zeros: true)
# => 13.01

Be careful, because precision means all digits after decimal point in this case.

要小心,因为精度意味着在这个例子中小数点后的所有数字。

#4


5  

I ended up with

我结束了

price = price.round(precision)
price = price % 1 == 0 ? price.to_i : price.to_f

this way you even get numbers instead of strings

这样你甚至可以得到数字而不是字符串

#5


2  

I just came across this, the fix above didnt work, but I came up with this, which works for me:

我刚刚遇到了这个问题,上面的修复没有起作用,但是我想到了这个,它对我很有用:

def format_data(data_element)
    # if the number is an in, dont show trailing zeros
    if data_element.to_i == data_element
         return "%i" % data_element
    else
    # otherwise show 2 decimals
        return "%.2f" % data_element
    end
end

#6


2  

Here's another way:

这是另一种方式:

decimal_precision = 2
"%.#{x.truncate.to_s.size + decimal_precision}g" % x

Or as a nice one-liner:

或者用一句俏皮话:

"%.#{x.truncate.to_s.size + 2}g" % x

#7


0  

Easy with Rails: http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_with_precision

容易使用Rails:http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html # method-i-number_with_precision

number_with_precision(value, precision: 2, significant: false, strip_insignificant_zeros: true)

#8


-2  

I was looking for a function to truncate (not approximate) a float or decimal number in Ruby on Rails, I figure out the follow solution to do that:

我在寻找一个函数来截断(不是近似地)Ruby on Rails中的浮点数或十进制数,我找到了下面的解决方案:

you guys can try in your console, the example:

你们可以在你们的控制台试试,例如:

>> a=8.88
>> (Integer(a*10))*0.10
>> 8.8

I hope it helps somebody. :-)

我希望它能帮助某人。:-)

#1


45  

You want to use %g instead of %f:

你想用%g而不是%f:

"%gx" % (factor / 100.00)

#2


26  

You can mix and match %g and %f like so:

可以这样混合和匹配%g和%f:

"%g" % ("%.2f" % number)

#3


15  

If you're using rails, you can use rails' NumberHelper methods: http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html

如果使用rails,可以使用rails的NumberHelper方法:http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html

number_with_precision(13.001, precision: 2, strip_insignificant_zeros: true)
# => 13
number_with_precision(13.005, precision: 2, strip_insignificant_zeros: true)
# => 13.01

Be careful, because precision means all digits after decimal point in this case.

要小心,因为精度意味着在这个例子中小数点后的所有数字。

#4


5  

I ended up with

我结束了

price = price.round(precision)
price = price % 1 == 0 ? price.to_i : price.to_f

this way you even get numbers instead of strings

这样你甚至可以得到数字而不是字符串

#5


2  

I just came across this, the fix above didnt work, but I came up with this, which works for me:

我刚刚遇到了这个问题,上面的修复没有起作用,但是我想到了这个,它对我很有用:

def format_data(data_element)
    # if the number is an in, dont show trailing zeros
    if data_element.to_i == data_element
         return "%i" % data_element
    else
    # otherwise show 2 decimals
        return "%.2f" % data_element
    end
end

#6


2  

Here's another way:

这是另一种方式:

decimal_precision = 2
"%.#{x.truncate.to_s.size + decimal_precision}g" % x

Or as a nice one-liner:

或者用一句俏皮话:

"%.#{x.truncate.to_s.size + 2}g" % x

#7


0  

Easy with Rails: http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_with_precision

容易使用Rails:http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html # method-i-number_with_precision

number_with_precision(value, precision: 2, significant: false, strip_insignificant_zeros: true)

#8


-2  

I was looking for a function to truncate (not approximate) a float or decimal number in Ruby on Rails, I figure out the follow solution to do that:

我在寻找一个函数来截断(不是近似地)Ruby on Rails中的浮点数或十进制数,我找到了下面的解决方案:

you guys can try in your console, the example:

你们可以在你们的控制台试试,例如:

>> a=8.88
>> (Integer(a*10))*0.10
>> 8.8

I hope it helps somebody. :-)

我希望它能帮助某人。:-)