Solved

Data conversion.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

856 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