-
August 12th, 2013, 01:48 AM
#1
Zero padding to a float number
Hi all,
I want to convert a float number like 2.3 to 0002.3.Is therany inbuilt function in C/C++ for this.Please give me some idea
-
August 12th, 2013, 02:00 AM
#2
Re: Zero padding to a float number
Perhaps you want to print it like that for formatting? If so, one option is to #include <iomanip> then write:
Code:
std::cout << std::setw(6) << std::setfill('0') << n << std::endl;
Another option is to use Boost.Format.
-
August 12th, 2013, 05:50 AM
#3
Re: Zero padding to a float number
I hve written in C
Code:
TCHAR *pBuf={"A2.3"};
float f=_wtof(pBuf++);
But f getting 0.00000(_wtof failure).What may be the reason?
-
August 12th, 2013, 06:26 AM
#4
Re: Zero padding to a float number
Originally Posted by manjut19
I hve written in C
Code:
TCHAR *pBuf={"A2.3"};
float f=_wtof(pBuf++);
But f getting 0.00000(_wtof failure).What may be the reason?
A2.3 is not a valid float number. For your _wtof, it takes the address pointed to by pBuf as the start of the number and then increments pBuf by 1. Try
Code:
float f = _wtof(++pBuf);
so that pBuf is incremented first to point to the 2 before _wtof is called.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 13th, 2013, 01:54 AM
#5
Re: Zero padding to a float number
Its working now.Thanks 2kaud
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|