As I said, I think using Number::Format is a good idea. In all cases, I would advise to consider that you have a (simple) number everywhere (even if it's actually a string, since perl will do the translation for you), and only add the formatting at the last moment when printing.

By default Number::Format works with an OO interface, but you can bypass that by adding the :subs flag. The :vars flag would be needed to change the parameters (eg: thousands separator) but the default values are actually the ones you want.

use Number::Format qw(:subs); print format_number(123456.789); # prints 123,456.79
I told you about the fact that perl will turn the string "123,456" into the number 123 (because it doesn't interpret thousands separator), Number::Format also provides a unformat_number to correctly translate such a string into the expected number.

Edit: my bad, to have trailing zeros you can use the optional parameters of format_number, which are the precision and trailing zeros:

use Number::Format qw(:subs); print format_number(123456, 2, 1); # print 123456 with two decimals, f +ill with 0s


In reply to Re^5: Issue getting value from text file and keep decimal by Eily
in thread Issue getting value from text file and keep decimal by jason.jackal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.