Solved

Data conversion.

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

744 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

15 Experts available now in Live!

Get 1:1 Help Now