473,507 Members | 8,022 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB->C conversion need help with formating function

140 New Member
Hi,

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
  4.  
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... :)
Thanks,
Sitko.
Oct 29 '07 #1
2 1729
Banfa
9,065 Recognized Expert Moderator Expert
how about using sprintf instead of fprintf?
Oct 29 '07 #2
sitko
140 New Member
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);
  3.  
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));
  3.  
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>);
  3.  
Thanks, for your help, I'll let the forum know if this works when I get to test this...
Sitko.
Oct 29 '07 #3

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

Similar topics

5
2611
by: Colin MacDougall | last post by:
Hi, I am just starting to learn Visual Basic 6 and was wondering if just going straight on to VB.NET would be a better bet. Is VB.NET just a newer version of Visual Basic 6 or does it differ...
19
2131
by: Raj Dhrolia | last post by:
Hi Guys, It might seem to be a very easy question, but i am very much eager to know some good technical difference between C# and VB.NET. Are there anything that i can do in one language and...
22
2502
by: ByteSize | last post by:
Dear All, Please, this is not meant to be offensive - but it is a challenge !!! I have posted on over a dozen so called 'vb.net' expert / blog sites - in the vain hope of finding a complete,...
3
1340
by: Philippe TEIRUH | last post by:
Hello, I have made a migration from VB 6.0 to VB.NET and noticed a great speed change when I access (from VB) to Excel Cells (same problem to write Visio formulas from VB). The same code takes...
132
5597
by: Kevin | last post by:
I don't know if I should even start this topic but here goes. I'm an ex vb6 developer, now developing in C#. The reason why I started developing in C# is because the company that I worked for at...
9
3107
by: Stanley | last post by:
I have a class "clsABC" which is created by VB.NET. I am trying use it in the C# program. Although I can add it in the project reference, I can't browse the methods and properties in the coding....
4
1490
by: Kannan | last post by:
Hi, We are planning to migrate one VB project to .Net. But I have a common questions. can any one clarify my below questions. 1. Is there is any advantage of using C# than VB.net. I feel both...
4
1462
by: Chris | last post by:
I am looking at this code in C# Assembly a=Assembly.LoadFrom("employee.dll"); Type t=a.GetType("Company.Employee"); I can't figure out what it is in VB.net can anyone help. I try Dim...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7105
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.