tags:

views:

151

answers:

3

Have a bit of code ported from VB6 to VB.NET. It uses Write and WriteLine all the way to produce output files.

Now, I need to compare outputs from original and ported code, but there's one tiny problem with number formatting. For instance whereas VB6 code Writes just .5, the VB.NET code produces 0.5, instead of .0005 (in original) it writes 5E-4, etc.

How can I make VB.NET's Write to write numbers in the same format as VB6? The option of refactoring writing code to include formatting step is not acceptable.

+4  A: 

In VB.NET, you could use the String.Format function in your Write/WriteLine calls.

Example:

Console.WriteLine(String.Format("{0:c}", price))

More information on formatting strings can be found here.

Jon
By the way, `Console.WriteLine` already has a overload that does the same as String.Format.
Pierre-Alain Vigeant
Thanks, but the code does not write to Console, it writes to files with `FileSystem.WriteLine` routine, an it does not have formatting options. Also, changing all calls from `Write(handle, arg)` to `Write(handle, String.Format("{0:c}", arg))` is the last option as I have too many `Write` and `WriteLine` calls.
zzandy
A: 

Have you checked out the function Microsoft.VisualBasic.Format?

Protron
A: 
  • Write and WriteLine are normally used with Input and LineInput therefore the data should still be able to be read and written in the same fashion.
  • If you are doing this because the output is for a third party program then you may just have to bite the bullet and search and replace using a simple recorded macro (Ctrl+Shift+R) or search and replace using (Ctrl+H). Neither of these is very difficult!
  • Welcome to .NET! When you say you ported from VB6 to VB.NET that means using new concepts and adapting new methods of doing things. Simply running the built in upgrade is not porting to .NET!
  • You might try something along the lines of Write(handle, arg.ToString("#.####")) instead of the aforementioned String.Format call.
  • You don't mention the reason why you need the data written in the same fashion as earlier?
  • Refactoring code is not acceptable..... hrm... care to explain that one? (If it's anything but laziness, people will listen.)
Converting a solution with 20K lines from VB to VB.NET is not as trivial as it would seem, thus I called it "porting".
zzandy
By saying that *refactoring is not acceptable* I meant that I don't have time to go though every call to `Write` and `WriteLine` and change it to `Write(handle, arg.ToString("#.####"))` to achieve a minor benefit.
zzandy
@zzandy - Not sure of the purpose of your first comment, but I have ported VB6 project with > 100k lines to .NET. Your second comment could be accomplished in a matter of seconds with a macro recorded in Visual Studio. Have you have ever recorded a macro in VS?
@roygbiv - Stack Overflow does not allow paragraphs in comments, so I used two comments where I normally would use two paragraphs. When you were porting that solution to VB.NET what tools did you used? I ran some free conversion tool (can't find it now) and than had spent a week or so to make it even compile. No I haven't recorded any macros for VS. The task it not relevant anymore, but I'll try macro.
zzandy