Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Right left middle string functions in C?

Posted on 2004-11-01
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

jacobhoover earned 2000 total points
ID: 12468840
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


Author Comment

ID: 12469005
Thanks, that did the trick perfectly!

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans

610 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