0

In the following program

#include <fstream>

int main(){
    
    
    std::ofstream fs_;
    fs_.open("test.txt");
    

   fs_<<1.f;
   fs_<<"   ";
   fs_<<1.0;
    
    fs_.flush();
    fs_.close();
    
return 0;   
}

Why the resulting file has written

1   1

instead of 1.0 1.0 ? And is there a way to write them as floats or doubles?

I should have added that with floats with a lot of decimals I don't want them to be truncated.

So far with the suggested solution 1.034255 becomes 1.03 with precision of 2

Rather than the "duplicate" that this question was closed for, this question is more related to this one

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • 1
    Try: `fs_< – Eljay Oct 07 '21 at 12:29
  • 1
    Thanks. `setprecision` dealt with the elimination of the decimals but as a side effect now truncates other floats to the number of decimals :( Is there a way to not truncate them? set the minimum precision somehow? – KansaiRobot Oct 07 '21 at 12:32
  • `instead of 1.0 1.0 ?` By default, trailing zeros are removed, with the dot. `floats with a lot of decimals I don't want them to be truncated.` Sooo `1/3f` is inifnite? For computer, most floats have endless decimals. `now truncates other floats to the number of decimals :( Is there a way to not truncate them?` Each float has endless decimals. You can print for exmaple with 40 decimals, and then parse the result as a string remove zeros up to the excluding the last one before `.`. – KamilCuk Oct 09 '21 at 00:10

0 Answers0