Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left of the dot precision

Tags:

c++

What I'm trying to is to write something like this

file << someFunction(5) << hex << 3;

And to get an output like this 00003 instead of 3 (and 300 will be 00300, etc)

Note:- Sorry if this was asked before, I don't know what term to search for to get this

EDIT:- I mean the zero's to the left, i included the hex part just to be clear I want it to be compatible with this format

like image 596
HElwy Avatar asked May 09 '13 13:05

HElwy


People also ask

What is the 4th decimal place called?

1 decimal place (tenths) 2 decimal places (hundredths) 3 decimal places (thousandths) 4 decimal places (ten-thousandths)

What is the 5th decimal place called?

the 5 is in the thousandths place.

What is the number to the right of the decimal point called?

The whole number is on the left side of the decimal point and the fractional part is on the right side of the decimal point. The decimal point makes it simple to read a decimal number.

What are decimal places called?

The first digit after the decimal represents the tenths place. The next digit after the decimal represents the hundredths place. The remaining digits continue to fill in the place values until there are no digits left.


3 Answers

I believe this is what you mean.

#include <iostream>
#include <iomanip>
int main()
{
   std::cout << std::setw(5) << std::setfill('0') << 3 << '\n';
   return 0;
}

Output

00003

Links setw, setfill.

Edit: Also, see std::internal at this question.

like image 78
BoBTFish Avatar answered Sep 23 '22 22:09

BoBTFish


Use

file << setfill('0') << setw(5) << 3;

to get 00003 instead of 3

like image 34
Oszkar Avatar answered Sep 23 '22 22:09

Oszkar


iomanip is what you need to search for.

#include <iomanip>

then

file << someFunction(5) << hex << setw(5) << setfill('0') << 3 << endl;
like image 35
parkydr Avatar answered Sep 21 '22 22:09

parkydr