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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:47:39+00:00 2026-06-18T14:47:39+00:00

I am finding some thing weird in number(float) to string conversion.. Here is the

  • 0

I am finding some thing weird in number(float) to string conversion..

Here is the sample code.

#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QString>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    qDebug()<<QString::number(50.5, 'f', 0);
    qDebug()<<QString::number(49.5, 'f', 0);

    return a.exec();
}

Here the output is

Starting /home/asit/qt/qstring1-build-desktop/qstring1...
"50" 
"50"

The output should be 51 and 50. Can somebody tell what is the reason behind this output ?

  • 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-18T14:47:40+00:00Added an answer on June 18, 2026 at 2:47 pm

    The problem with floating point numbers is that they cannot be
    represented exactly. So 49.5 might be stored as a number slightly more than 49.5. The same applies to 50.5, but this one might be stored as a number slightly less than 50.5.

    This has bitten me before on Linux where you try and cast a
    double to an int. Take e.g.

    double value = 0.3;
    int result = static_cast<int> (value * 1000);
    

    On e.g Solaris SPARC you will get 300 as you expect. With
    gcc on Linux you get 299. Why? Well, although both
    convert the double to an int by rounding down, on gcc on
    Linux the double is assigned to a FPU 80 bit register
    where it is represented by a number slightly less than
    0.3. On Solaris (and in fact most other systems, including
    VC++) the double is assigned to a 64 bit register
    where it is represented as slightly bigger then 0.3.

    If you want to be sure that you will have rounded your number to correct value, just add 0.5 before casting or use qRound() which will make that for you.

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

Sidebar

Related Questions

I've recently written some Scala code which processes a String, finding all its sub-strings
I am manually converting code from Java (1.6) to C# and finding some difficulty
I am finding some weird invisible character quotes in firebug when I inspect the
Again I'm moving from Xcode 3 to 4 and finding that some things are
I'm finding some problems with a query that returns the sum of a field
I am using matplotlib and I'm finding some problems when trying to plot large
I am converting my Objective-C Cocos2d project into ARC and I am finding some
I've been playing with jQuery in an ASP.NET project and am finding some odd
I am a beginner in UNIX. I am finding some difficulty in input/output redirection.
I'm writing a particle system manager with SlimDX/D3D11, and I'm finding some problems with

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.