Solved

Data conversion.

Posted on 2001-06-04
4
202 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

  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 …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

726 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