The issue is that you go back and forth between number and string representation. In $main_balance = $main_balance - $debit; the minus is a numeric operation, so perl translates your string into numbers, where the number of displayed digits is not memorized. Your format function turns it back into a string, but the formatting already has been lost.

I think your best option is Number::Format, which will output the expected format wheter the input is already a number, or a string representation (if the string doesn't already includes thousands separator, "900,000" will be interpreted as the number 900).

Also the sigil @ is used to fetch several elements, and $ a single one. So even if you are using a single element from an array, $ has to be used (see perldata). The second element of the array @fields should therefore be $fields[1] instead. Perl will actually complain about this if warnings are activated (which they should be).

Edit: turned $fields1 into $fields[1], thanks AnomalousMonk


In reply to Re: 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.