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
- Private Function Format10(ByVal dblNumber As Double, ByVal intDecimal As Integer) As String
- Format10 = CStr(FormatNumber(dblNumber, intDecimal, , , TriState.False)).PadLeft(10)
- End Function
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... :)
Thanks,
Sitko.