Formatting the decimals

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Formatting the decimals

    Dear Friends,

    How do I format the decimals, i.e, if there is no
    decimal part, then add .00, if there is one decimal,
    then add '0'.

    For eg., how to convert 123 to 123.00
    and 123.5 to 123.50.

    Thanks in advance,
    Mallik.
    Mallik Guest

  2. Similar Questions and Discussions

    1. Formatting decimals with 2 digits - 3.70 vs 3.7000 ???
      I am formatting decimals on several columns within my DataGrid. For the most part it is working correctly. However, 2 of the columns will not...
    2. CFINPUT and decimals
      Hi.. I am encountering an odd problem... I have a series of CFINPUT statements in a form...The table that the data for the form comes from...
    3. Decimals not Getting into ASP
      We have an ASP page that grabs data from an Oracle 9i table and then dumps it into another. It had been working great until we swapped out servers....
    4. how to keep decimals from rounding
      Is there a way to surpress round() or number_format() from rounding decimal places? I have a value of ".253338" being returned via a calc...
    5. Formatting Decimals in a Combo Box or List Box
      I am having trouble with a couple of things. On separate forms, even different databases I have either a control that is a combo box or a field in...
  3. #2

    Default Re: Formatting the decimals


    On Thu, 29 Jan 2004, Mallik wrote:
    > How do I format the decimals, i.e, if there is no
    > decimal part, then add .00, if there is one decimal,
    > then add '0'.
    >
    > For eg., how to convert 123 to 123.00
    > and 123.5 to 123.50.


    sprintf

    Try this
    -------------------------------------------------
    #!/usr/bin/perl -w

    while(<DATA>){
    chomp;
    $formatted = sprintf ("%0.2f",$_);
    print "$formatted\n";
    }

    __DATA__
    125
    123.5
    2
    2.3456
    34.6


    Owen Cook Guest

  4. #3

    Default Re: Formatting the decimals

    Mallik wrote:
    >
    > Dear Friends,
    Hello,
    > How do I format the decimals, i.e, if there is no
    > decimal part, then add .00, if there is one decimal,
    > then add '0'.
    >
    > For eg., how to convert 123 to 123.00
    > and 123.5 to 123.50.
    printf "%.2f\n", $_ for 123, 123.5;


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  5. #4

    Default Re: Formatting the decimals

    Owen is dead on, but I think we can do that with less code. Try
    something like:

    printf ("%0.2f\n", $_) while (<>);

    Regards,
    Adam

    On Jan 29, 2004, at 5:25 AM, Owen Cook wrote:
    >
    > On Thu, 29 Jan 2004, Mallik wrote:
    >
    >> How do I format the decimals, i.e, if there is no
    >> decimal part, then add .00, if there is one decimal,
    >> then add '0'.
    >>
    >> For eg., how to convert 123 to 123.00
    >> and 123.5 to 123.50.
    >
    > sprintf
    >
    > Try this
    > -------------------------------------------------
    > #!/usr/bin/perl -w
    >
    > while(<DATA>){
    > chomp;
    > $formatted = sprintf ("%0.2f",$_);
    > print "$formatted\n";
    > }
    >
    > __DATA__
    > 125
    > 123.5
    > 2
    > 2.3456
    > 34.6
    Adam Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139