470,614 Members | 1,572 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,614 developers. It's quick & easy.

VB->C conversion need help with formating function

140 100+

I'm in the process of converting a VB.net 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 VB.net for granted, now that I've been working on this conversion... :)
Oct 29 '07 #1
2 1560
9,065 Expert Mod 8TB
how about using sprintf instead of fprintf?
Oct 29 '07 #2
140 100+
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.

Similar topics

5 posts views Thread by Colin MacDougall | last post: by
19 posts views Thread by Raj Dhrolia | last post: by
3 posts views Thread by Philippe TEIRUH | last post: by
132 posts views Thread by Kevin | last post: by
9 posts views Thread by Stanley | last post: by
4 posts views Thread by Kannan | last post: by
4 posts views Thread by Chris | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.