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?
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.
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.
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 "
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With