There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen blue screen blue screen of death boot bsod computer connection crash css dell display driver drivers email error excel firefox firefox 3 hard drive internet internet explorer itunes laptop linux malware monitor network networking outlook outlook 2003 outlook express partition password printer problem problems ram router security slow sound trojan usb video virus vista windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
C++ Help


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
neveranything's Avatar
Junior Member with 2 posts.
 
Join Date: Jan 2002
31-Jan-2002, 09:28 PM #1
Question C++ Help
I need a little help with my C++ programming. I don't know how to get a variable to round to the nearest cent.

I'm using double type variables for my decimals. Is there a way to format them for currency or do I need a different variable.
MacFromOK's Avatar
Senior Member with 1,947 posts.
 
Join Date: Mar 2001
Location: Oklahoma
Experience: idiota de la aldea
01-Feb-2002, 06:35 AM #2
Hi,

I assume you're working in DOS...

This will print the number with 2 decimal places:
Code:
#include &ltstdlib.h>
#include &ltstdio.h>

int main()
{
  double number = 1.6759;

  // Using %.2f  to remove unwanted digits.
  printf("%f rounds to %.2f",number,number);
  return 0;
}
This will actually round the number off to 2 decimal
places, in case you need to do something with it:
Code:
#include &ltstdlib.h>
#include &ltstdio.h>

int main()
{
  double number = 1.6759;
  char *tmp;

  sprintf(tmp, "%.2f", number);
  number = atof(tmp);

  // Still using %.2f  to remove trailing zeroes.
  printf("Number is %.2f",number);
  return 0;
}
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 10:19 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.