If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
array formatting
array formatting
How to format array of floats using ruby?
Equivalent of "format string" primitive.
For example trim array values to 0.00 or set display like 000.00 and so on.
Equivalent of "format string" primitive.
For example trim array values to 0.00 or set display like 000.00 and so on.
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Feel free to donate. Thank you for your contribution.
- trogluddite
- Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: array formatting
...is the 'format' method. It uses exactly the same format codes as the primitive - which you give as the first argument (in the form of a string), followed by the value that needs formatting... e.g.tester wrote:Equivalent of "format string" primitive...
Code: Select all
@x = Math::PI
format( "%.3f", @x) #=> "3.142"Code: Select all
format("The value of PI is %.3f approximately", @x) #=> "The value of PI is 3.142 approximately"Code: Select all
@x = 5.12345
@y = @x * @x
format("The square of %.3f is %.3f", @x, @y) #=> "The square of 5.123 is 26.250"Code: Select all
@myArray = [1, 2, 3, 4, 5]
@format_code = "%.3f"
@formatted = @myArray.collect{|item| format(@format_code, item)}All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Don't stagnate, mutate to create!