help float to string

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

  1. #1

    Default RE: help float to string

    try the sprintf

    print sprint("%.02f", $f);

    if I'm not rong;



    -----Original Message-----
    From: [email]perl@swanmail.com[/email] [mailto:perl@swanmail.com]
    Sent: Tuesday, October 14, 2003 10:18 AM
    To: [email]beginners@perl.org[/email]
    Subject: help float to string


    Can someone help me with retaining the precisions when they are zero?

    ie -

    $f = 9.00
    $z = $f;
    print $z; #result in 9 - bad I want 9.00

    $fx = 9.25
    $z = $fx;
    print $z; #result in 9.25 - good

    I can but don't want to use a sub like itoa. Is there a better way to
    convert to string?

    $z=itoa($f);
    print $z #result 9.00
    $z=itoa($fx);
    print $z #result 9.25

    sub itoa { return sprintf("%.2f", $_[0]); }



    -----------------------------------------
    eMail solutions by
    [url]http://www.swanmail.com[/url]

    --
    To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    Marcos Rebelo Guest

  2. Similar Questions and Discussions

    1. #39126 [NEW]: String->float->String conversion behavior
      From: bobson at rpg dot pl Operating system: Linux PHP version: 5CVS-2006-10-11 (snap) PHP Bug Type: Unknown/Other Function...
    2. #25562 [Opn->Bgs]: Float to String to Float conversion error
      ID: 25562 Updated by: helly@php.net Reported By: daseymour at 3hc dot org -Status: Open +Status: ...
    3. #25562 [NEW]: Float to String to Float conversion error
      From: daseymour at 3hc dot org Operating system: and PHP version: 4.3.3 PHP Bug Type: Math related Bug description: Float...
    4. How to convert string to float?
      i want to covert a querystring to float number in asp. what function should i use? i know cint() can convert string to int but how about float?
    5. preventing string conversion to float.
      It has been a long time since I touched any perl, and I am having a problem with parsing a text file. I need to pull out a string in the form...
  3. #2

    Default help float to string

    Can someone help me with retaining the precisions when they are zero?

    ie -

    $f = 9.00
    $z = $f;
    print $z; #result in 9 - bad I want 9.00

    $fx = 9.25
    $z = $fx;
    print $z; #result in 9.25 - good

    I can but don't want to use a sub like itoa. Is there a better way to
    convert to string?

    $z=itoa($f);
    print $z #result 9.00
    $z=itoa($fx);
    print $z #result 9.25

    sub itoa { return sprintf("%.2f", $_[0]); }



    -----------------------------------------
    eMail solutions by
    [url]http://www.swanmail.com[/url]
    perl@swanmail.com Guest

  4. #3

    Default Re: help float to string


    [email]perl@swanmail.com[/email] said:
    > Can someone help me with retaining the precisions when they are zero?
    >
    > ie -
    >
    > $f = 9.00
    > $z = $f;
    > print $z; #result in 9 - bad I want 9.00
    >
    > $fx = 9.25
    > $z = $fx;
    > print $z; #result in 9.25 - good
    >
    > I can but don't want to use a sub like itoa.
    Why not? Seems like the perfect solution to me.
    > Is there a better way to
    > convert to string?
    That depends on your definition of better.

    $ perl -le '$# = "%.2f"; print 9'
    9.00

    But $# is deprecated, so don't do that.

    $ perl -le '$f = "9.00"; $z = $f; print $z'
    9.00
    > $z=itoa($f);
    > print $z #result 9.00
    > $z=itoa($fx);
    > print $z #result 9.25
    >
    > sub itoa { return sprintf("%.2f", $_[0]); }
    --
    Paul Johnson - [email]paul@pjcj.net[/email]
    [url]http://www.pjcj.net[/url]

    Paul Johnson 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