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 7760245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:49:54+00:00 2026-06-01T13:49:54+00:00

could soemone help me with the following condition, please? I’m trying to compare $price

  • 0

could soemone help me with the following condition, please?
I’m trying to compare $price and $lsec.

if( (sprintf("%.2f", ($price*100+0.5)/100)*1 != $lsec*1) )
{
                print Dumper($price,$lsec)
}

Sometimes the dumper prints same numbers(as strings) and jumps in.
Thought, that multiplying with 1 makes floats from them…

Here dumper output:

$VAR1 = '8.5';
$VAR2 = '8.5';

What am I doing wrong?

Thank you,

Greetings and happy easter.

  • 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-01T13:49:55+00:00Added an answer on June 1, 2026 at 1:49 pm

    You are correct to say that multiplying a string by 1 will force it to be evaluated as a number, but the numeric != comparator will do the same thing. This is presumably a technique you have acquired from other languages as Perl will generally do the right thing and there is no need to force a cast of either operand.

    Lets take a look at the values you’re comparing:

    use strict;
    use warnings;
    
    use Data::Dumper;
    
    my $price = '8.5';
    my $lsec = '8.5';
    
    my $rounded_price = sprintf("%.2f", ($price * 100 + 0.5) / 100);
    print "$rounded_price <=> $lsec\n";
    
    if ( $rounded_price != $lsec ) {
      print Dumper($price,$lsec);
    }
    

    output

    8.51 <=> 8.5
    $VAR1 = '8.5';
    $VAR2 = '8.5';
    

    So Perl is correctly saying that 8.51 is unequal to 8.5.

    I suspect that your

    ($price * 100 + 0.5) / 100
    

    is intended to round $price to two decimal places, but all it does in fact is to increase $price by 0.005. I think you meant to write

    int($price * 100 + 0.5) / 100
    

    but you also put the value through sprintf which is another way to do the same thing.

    Either

    $price = int($price * 100 + 0.5) / 100
    

    or

    $price = sprintf ".2f", $price
    

    but both is overkill!

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

Sidebar

Related Questions

Could someone please help with the following jQuery I need all jQuery UI buttons
Could someone help me please. I am trying to use JQGrid to dynamically render
Please could someone help with the following: Using PHP I want to be able
Could someone help me clarify the following conecpts, and the relationship among them ?
I was hoping someone could help me with the following script: jQuery(document).ready(function($) { $(.infoBoxBtn
Could someone help me with building the following query. I have a table called
I got segmentation fault for the following code, could someone help me understand why?
Could someone help me create some javascript to perform the following behavior. Upon the
Could someone help me understand what is going on in the following Python code
Could someone help me resolve the discrepancies between the following two cases. In both

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.