I am trying to write a function that passes in a int and if int is <10 puts a 0 infront of the int to print out. I can't use the manipulator.
Instead of 3, it prints 03.
Does anyone have any ideas?
5 13320
I have to pass mutilple numbers into this function. So I don't think it can be in the out statement. I would think that it would have to be assigned to a variable.
Hi
If your function doesnt have to return string composed of numbers you passed to it, in another words If you just need to print out the numbers to screen I would recommend you:
in C use printf :
int i = 5, j = 15;
printf("%02d", i);
printf("%d", j);
in C++ you can use cout in your function: - #include <iostream>
-
using namespace std;
-
-
void print(const int& x) {
-
cout << (x < 10 ? cout << 0, x : x);
-
}
-
-
int main() {
-
-
print(5);
-
cout << endl;
-
print(15);
-
-
return 0;
-
}
For A number, say A, with N decimal digits, we can relate them by N = (int)(log A), where the logarithm is base 10.
If you want all numbers to have 30 digits, then print a string with (30-N) "0"s, followed by your number A.
thanks for good idea D_C
I was actually thinkin how to get count of digits of integer number.
(int)log10 works fine, so I upgraded my function:
void print(const int& x, int n) {
while(--n > (int)log10(x)) cout << 0;
cout << x;
}
I found this post on google while looking for a solution to the same problem.
Here's what I got,
cout.fill('0');
cout<<std::setw(N)<<your number<<endl;
Where N is the width of the string you wish to print, and your number is your int.
The fill function tells the stream what to fillt he extra spaces with.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Steve |
last post by:
Is there a way in PHP to zero fill an integer to a specified length?
For instance, if I have a two digit number, and I want to zero fill it
to four digits, is there a PHP function I can use? Or do...
|
by: simondex |
last post by:
Hi, Everyone!
Does anyone know how to initialize an int array with a non-zero number?
Thank You Very Much.
Truly Yours, Simon Dexter
|
by: John Baker |
last post by:
Hi:
I have a report that has currency fields which I wish to have as blank if they are zero,
but to show as fill currency if they have data in them:
0 = Blank
100= $ 100.00
..01 = $ .01
...
|
by: Steve |
last post by:
I have a database where I want to use an autonumber field to make SKUs
for a new product when it is entered. Is there a way I can make the
field zero fill to a certain length? For instance, if I...
|
by: Zhiqiang Ye |
last post by:
Hi, All
I am reading FAQ of this group. I have a question about this:
http://www.eskimo.com/~scs/C-faq/q7.31.html
It says:
"
p = malloc(m * n);
memset(p, 0, m * n);
The zero fill is...
|
by: Lyle Fairfield |
last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp
"If the value of the variant argument is Null, the Nz function returns
the number zero or a...
|
by: Haleigh |
last post by:
I just started a few days ago, so I'm very new to this.
I'm trying to update a gridview using a store procedure. When I am in the update subroutine, newvalues and oldvalues are empty, plus count is...
|
by: Bob |
last post by:
Hi all,
I have some additions that I need to do on some fields in some of my
queries but it seems that my fields don't always have a value in
them...
So for example, I want the result of ColA...
|
by: tshad |
last post by:
Is there a way to zero fill a numeric into a string that is a fixed lengh of
10 chars?
In Sql I would do something like: Right("0000000000" +
Convert(varchar,amt),10).
Is there an easy way to...
|
by: lawrence k |
last post by:
I've got a function that starts off like this:
function alphabeticalListOfAlbumsByLetter($whichLetter="a") {
}
I pass in the letters (a, b, c, d, etc) and the numbers (0, 1, 2, 3,
4,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |