Results 1 to 2 of 2

Thread: Formatting for two decimal places

  1. #1
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Formatting for two decimal places

    Hello everyone,

    I am making a simple interest calculator. My problem is that when a number, such as the payoff total, ends in 0, that 0 doesn't show.

    The code used for rounding to two decimal places:
    Qt Code:
    1. double r_total = static_cast<double>(static_cast<int>(total*100+0.5))/100.0;
    To copy to clipboard, switch view to plain text mode 

    The code used for converting to a string:
    Qt Code:
    1. QVariant tmp(r_total);
    2. QString s_total = tmp.toString();
    To copy to clipboard, switch view to plain text mode 

    I've been racking my brain trying to come up with a way to show that 0, so it shows "$55.20" instead of "$55.2", but can't seem to come up with anything?

    Does anyone have any ideas?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Formatting for two decimal places

    Qt Code:
    1. double val = 1.2;
    2. QString str= QString::number(val, 'f', 2);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following 2 users say thank you to wysota for this useful post:

    TomJoad (4th April 2011), vitor13almeida (3rd August 2017)

Similar Threads

  1. Rounding a float to X decimal places
    By qlands in forum General Programming
    Replies: 2
    Last Post: 22nd June 2010, 21:02
  2. My Network Places in QFileDialog
    By AlHadr in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2009, 15:38
  3. Best places to submit your freeware?
    By tgreaves in forum Installation and Deployment
    Replies: 18
    Last Post: 8th March 2009, 21:20
  4. QTableWidget and Decimal places
    By pshah.mumbai in forum Qt Programming
    Replies: 3
    Last Post: 17th August 2008, 08:30
  5. converting the value from hexadecimal to decimal value
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 16th November 2007, 10:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.