Right left middle string functions in C?

Posted on 2004-11-01
Last Modified: 2010-04-17
I'm coding something in C and I have a number of variable size up to 3 digits, that I would like to convert to a float. For example, I have 250, I'd like it to show up as 2.50 or if I have 5 I'd like it to show as .05.
What possible ways can I do that? I had thought of sprintf() to make it a string and format it, but I can't figure out how to pull the the numbers to format it with a . in the right spot. atof() ended up with things like 250.00.  Thanks in advance!
Question by:Giza
    LVL 7

    Accepted Solution

    Hmmm.... assuming you want the decimal always to be in the same spot...

    float fBase = atof(szVarSize3);
    fBase = fbase/100; // OK, got the "correct number"
    char szOut[5];
    sprintf(szOut, "%.2f", fBase); // String version of it

    LVL 1

    Author Comment

    Thanks, that did the trick perfectly!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    A short article about problems I had with the new location API and permissions in Marshmallow
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now