CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2013
    Posts
    77

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2013
    Posts
    77

    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?

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Zero padding to a float number

    Quote Originally Posted by manjut19 View Post
    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)

  5. #5
    Join Date
    Apr 2013
    Posts
    77

    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
  •  





Click Here to Expand Forum to Full Width

Featured