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

  • Home
  • SEARCH
  • 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 8764335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:00:35+00:00 2026-06-13T16:00:35+00:00

I looked for a while and couldn’t find an answer on here, but it

  • 0

I looked for a while and couldn’t find an answer on here, but it seems to be a kind of weird question. I’m working with the fstream library in C++. What I’m trying to do is take data from an input file, assign it variables, and output this to both the screen and the output file. This is all for a project i’m working on that calculates the monthly payment of the car loan, which is why the variables are named the way they are.

My data file looks like this:

105670.00 12345.00 0.057 4

and essentially what is happening is I am losing everything after the decimal of the first two numbers (regardless of what i put as the decimal), but it doesn’t happen to the 3rd number. additionally, when i try to setprecision(2) of the first two numbers i get a strange logic error, which i will show after my code.

my code looks like this:

#include<fstream>
#include<iomanip>
#include<iostream>
using namespace std;

int main ()
{
ifstream din; // These are my input and output files
ofstream dout;
float purchasePrice; // The first number of the input file.
float downPayment; // Second number.
float annualInterest; // Third number.
float numYears; // Last number.

// declaring the input/output files code here

din >> purchasePrice >> downPayment >> annualInterest >> numYears;

cout << purchasePrice << endl;
cout << downPayment << endl;
cout << annualInterest << endl;
cout << setprecision(2) << purchasePrice << endl;
cout << setprecision(2) << downPayment << endl;
cout << setprecison(2) << annualInterest << endl;
}

and here is my output:

105670
12345
0.057
1.1e+005
1.2e+004
0.057

I want my output to be:

105670.00
12345.00
0.057
105670.00
12345.00
0.05

Additionally, when performing any calculations, the numbers act as if they still have everything after the decimal. My question is, why are only some of the floats truncating, and why does setprecision() not work the way it is expected in this case?

Any responses are greatly appreciated, I’m sorry its such a lengthy explanation.

  • 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-13T16:00:36+00:00Added an answer on June 13, 2026 at 4:00 pm

    You also need to set the fmtflags to fixed:

    std::cout << std::fixed << std::setprecision(2) << purchasePrice << "\n";
    

    Without specifying scientific or fixed, the output mechanisms will use whatever scheme is deemed to be best. You know what’s “best”, and that’s a fixed point scheme. For more detail, see http://en.cppreference.com/w/cpp/io/manip .

    Even better is not to use floating point numbers at all when dealing with money. It is far better to use some fixed point arithmetic package.

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

Sidebar

Related Questions

I looked around and couldn't find it! Maybe I didn't look hard enough,but this
(I found the answer to this elsewhere while writing the question, but I thought
I have searched a lot for this issue. But couldn't find the exact answer.
I've looked around the current answers for similar questions but still couldn't find the
This was something I looked at a while back but couldn't figure it out.
I did some searching on here and couldn't find the answer I exactly needed.
I tried googling this, couldn't find an answer, searched here, couldn't find an answer.
I've looked on the web for a while and can't seem to find the
I looked through Stackoverflow and found almost identical question here . It was asked
I looked through other questions and couldn't find what I needed. Best if I

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.