Zero padding to a float number
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Zero padding to a float number

  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,705

    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
    4,921

    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. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on.
    C, C++ Compiler: Microsoft VS2015

  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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center