Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Ruby have any number formatting classes?

Tags:

ruby

Does Ruby have any Formatter classes or methods that can be used to format numbers for things like currency, etc., or are there any gems that do this, or do you have to write you own?

like image 661
ab217 Avatar asked Nov 13 '10 23:11

ab217


People also ask

What is format in Ruby?

In this example, %d is the format specifier (here is a list of available specifiers) and time is the variable we want formatted. A %d format will give us whole numbers only. If we want to display floating point numbers we need to use %f. We can specify the number of decimal places we want like this: %0.2f.

What is printf in Ruby?

Ruby - String printf FormatRuby printf method can output "format strings" containing specifiers starting with a percent sign (%). The format string may be followed by one or more data items separated by commas. The list of data items should match the number and type of the format specifiers.


1 Answers

Ruby has all the standard print formatters, available either via printf, sprintf or using 'formatstring' % [var1, ...].

>> '%.2f' % 3.14159 #=> "3.14" >> '%4s %-4s' % ['foo', 'bar'] #=> " foo bar " 
like image 90
the Tin Man Avatar answered Sep 19 '22 16:09

the Tin Man