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

x
?
Solved

Data conversion.

Posted on 2001-06-04
4
Medium Priority
?
215 Views
Last Modified: 2010-04-02
strncpy(strPrincipal,FileBuffer+99,11);
int intPrincipal ;
intPrincipal = atoi(strPrincipal);
setprecision(2);
dblPrincipal = intPrincipal*(1/100.0);

Actually I am trying to set the format 999999999.99
total 11. I am copying into the char array strPrincipal.
now I want to set this value to the above format .

Can any one rectify my code

Thanks in advance.

Jack
0
Comment
Question by:jack_2001
[X]
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
  • 3
4 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 6153291
It's not at all clear to me what you are trying to do.  You code doesn't shed much light on this for me either.

Perhaps a better explanation regarding what you are trying to do.

Also:

strncpy(strPrincipal,FileBuffer+99,11);

The above line of code is VERY DANGEROUS.  Note that strncpy does NOT terminate your string.  Depending on what is already in strPrincipal your results could be indeterminate or even disastrous.  If you must use such a thing, I'd suggest:

memset(strPrincipal, 0, 12);
strncpy(strPrincipal,FileBuffer+99,11);
0
 
LVL 30

Expert Comment

by:Axter
ID: 6153298
The following code will put the dblPrincipal number into the data string, and use the format you specified above.
char data[20];
sprintf(data,"%i9.2",dblPricipal);
0
 
LVL 30

Expert Comment

by:Axter
ID: 6153302
Correction:
char data[20];
sprintf(data,"%9.2f",dblPricipal);
0
 
LVL 30

Accepted Solution

by:
Axter earned 150 total points
ID: 6153307
When using sprintf/printf you can specify the format by putting a decimal in between the % and the type.
Then put the number of digits you want to the left and right of the decimal in the associated positions.
0

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

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