laziness, impatience, and hubris | |
PerlMonks |
Re: More elegant way than multiple "if"?by GrandFather (Saint) |
on Nov 28, 2019 at 20:53 UTC ( [id://11109393]=note: print w/replies, xml ) | Need Help?? |
Your instinct to avoid a cascade of if statements is correct. That leads to hard to maintain code. There are some subtleties for a general solution however and in a production context you would want to write a test suite before you start implementing a solution - not least so that you have a good idea of where all the edge cases are. The following code uses a couple of functions to change to and from float values:
Prints:
Note that K got changed to k. This code would be fairly easy to extend to handle standard SI suffixes, but that is left as an exercise for the learner.
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
In Section
Seekers of Perl Wisdom
|
|