Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8052695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:36:47+00:00 2026-06-05T07:36:47+00:00

I have the below Perl function to display up to two decimals places. It’s

  • 0
  1. I have the below Perl function to display up to two decimals places. It’s not working when the input value is 2.01, and it gives the output as 2 instead of 2.01. I am not sure why it’s rounding.

Instead of printf I wrote the output to a file, but still it gives me output1 as 2.

    my $ramount = 2.01;
    $ramount = int($ramount*100)/100;
    printf "output1: $ramount";
  1. If I have values like .2, .23, .2345, 1,23, 23.1, and 9, what function can I use to pad zeros so that it displays 0.2, 0.23, 0.2345, 1, 23, 23.1, and 9?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:36:48+00:00Added an answer on June 5, 2026 at 7:36 am

    I think this sequence will answer your question:

      DB<1> $a=2.01
    
      DB<2> p $a
    2.01
      DB<3> printf "%20.10f\n", $a
            2.0100000000
    
      DB<4> printf "%20.16f\n", $a
      2.0099999999999998
    
      DB<5> printf "%20.16f\n", ($a*100)
    200.9999999999999716
    
      DB<6> printf "%20.16f\n", ($a*100)/100
      2.0099999999999998
    
      DB<7> printf "%20.16f\n", int($a*100)
    200.0000000000000000
    
      DB<8> printf "%20.16f\n", int($a*100)/100
      2.0000000000000000
    
      DB<9>
    

    Essentially (and this has been answered many times on SO), 2.01 cannot be represented EXACTLY as a floating point number. The closest possible float is, as you see above, 2.009999999999999716…

    As to padding, try

    printf "%04d", $number
    

    The leading zero in the format tells printf (or sprintf) to left-pad with zero.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have below html code in my aspx. <input type=hidden id=medicalLink value='<a href=/forms/contactus.aspx >Contact
My Perl Script retrieves the argument in the below way. Have Getoptions function to
I have a Perl script which nests foreach loops as seen below. It takes
Could you please have a look at my code below. #!C:\Perl\bin\perl.exe use strict; use
I have below file : Arijit Ghosh.1.100.0 Arindam Roy.3.4.678 Now i want the output
I have this perl code below, basically want to loop through these 2 files.
I have the below string on perl : my $string = xyz;1;xyz;2;a;2;b;2 i want
I have a strange problem in matching a pattern. Consider the Perl code below
I have a perl object (reference to array of references) like the below: my
I have the below Perl code to make a copy of a binary file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.