By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,089 Members | 2,069 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

VB->C conversion need help with formating function

P: 140

I'm in the process of converting a program into a C program so it can run on a unix like machine.

I've been moving along at a nice pace, but this conversion has stumped me.

I need a function to take two arguments:(a double number which may or may not be an integer, and an integer which will be the number of digits to store after the decimal place) it needs to turn this into a string which is always 10 characters long. if the number is less than 10 characters, it would be leading spaces. Here is the VB code:
Expand|Select|Wrap|Line Numbers
  1. Private Function Format10(ByVal dblNumber As Double, ByVal intDecimal As Integer) As String
  2.         Format10 = CStr(FormatNumber(dblNumber, intDecimal, , , TriState.False)).PadLeft(10)
  3.     End Function
Some examples of input/output: ('b'=space)
Format10(100.0, 4) -> "bb100.0000"
Format10(100.0, 0) -> "bbbbbbb100"
Format10(100.3333, 2) -> "bbbb100.34"

I was working towards using fprintf's (and %fX.Y), to come up with my formating, but couldn't figure out how to send that formating to a char-array(string). If it helps, every time the function is used its related to a fprintf line, so I also toyed with just formating a the variables at out printing time, but can figure out how to tack on the leading spaces which would keep it at 10 characters. If I was doing this in VB (besides using the above function, I would of figured out the length of the number, and add spaces to the beginning until the total length of the string = 10...and am having trouble shaking this algorithm from my head...

I'm definitely never going to take for granted, now that I've been working on this conversion... :)
Oct 29 '07 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 5K+
P: 8,916
how about using sprintf instead of fprintf?
Oct 29 '07 #2

P: 140
Yeah, I was looking into sprintf, which for some reason, wasn't mentioned as a potential alternative to printf/fprintf, from my sources...

But, I think I've figured it out (haven't ran it yet):

I did a bit more analysis, and found that the second number (numofdigits is set to 0 in nearly every use of this function, and is set to 2 or 3 only twice in the code, so I did it like this:
Expand|Select|Wrap|Line Numbers
  1. char * str = "%10d\n";
  2. fprintf(datFile, str, intIndex);
Which even is less code than the other way. When there are multiple calls, in one line I do this:
Expand|Select|Wrap|Line Numbers
  1. char * str = "%10d%10d         0\n";
  2. fprintf(datFile, str, (10000*intIndex+1),(intIndex*100+1));
When its set to 2 or 3 I'll just hard code whichever one it is, like this:
Expand|Select|Wrap|Line Numbers
  1. char * str = "%10.2f\n";
  2. fprintf(datFile, str, <doublenumber>);
Thanks, for your help, I'll let the forum know if this works when I get to test this...
Oct 29 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.