Solved

Data conversion.

Posted on 2001-06-04
4
193 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
  • 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 50 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

776 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