Valhalla Legends Forums Archive | .NET Platform | Fancy Formatting C# Lesson #1 of 2

AuthorMessageTime
DecA
[u] Fancy Formatting C# By:Deca[xL]  Lesson #1 of 2[/u]
There is going to be a follow up lesson to this with more advanced Features.
[color=Yellow]Note: this is just to give you a small understanding of what im going to do in the next lesson[/color]
__________________________________________________________________________________

This is a begginer(s) level tutorial, i am writing this tutorial to help out the people new to C#.
If you have any qustions after this tutorial is done, feel free to IM me(xlDecalx) and i will try to answer any qustion you might have, the best to my ability.

In this tutorial, i will be using Hungarian Notation, this a way of better identifying variables. In large projects, it can become hard to identify your variables, as to what data type they are, so that is the soul reason of using this Notation. Here is the table of value..

=Hungarian Notation=
byte = by
short = s
int = i
long = l
decimal = m
string = str
float = f
double = d   

Now this next table i am going to show you, is the Formatting table...It will show you all the different format letters, meanings, and descriptions..

__________________________________________________________________________________
Letter Meaning Description

C        Curenncy          A curenncy sign is diplayed, and thousands are sperated
E        Exponential      The number is displayed in scientific notation.
F        Fixed Point      The nubmer is not displayed in scientific notaion.
G      General            Expoential or ScientificNotation
N        Number            The same as F but the thousands are sperated with a comma
P        Percent            The numer is multiplied by 100, a percent sign is displayed and thoughsands are seperated with a comma.
X      Hexadecimal    (integer only) Numbers are displayed in hexadecimal
__________________________________________________________________________________

I am going to assume you guys took the time to read over these tables and have a good understanding of what each means. So let's begin.

As you know, if you read the tables, m  is a decimal, so the expression, m.ToString();  would convert the decimal  to a string. So...now we are going to explore some of the flexibility of this method, such as the format. Lets start with G, If you were to add G to m.ToString("G"); statement
it would not have any affect on the output, Because G stands for general format. so let's put this all togeather.
[code]
using System;

namespace FancyFormattingG
{
class GeneralFormat
{

Static void Main()
{
int iA = 100;
Console.Write(iA.ToString("G")); //remeber the quots.
}
}
}
[/code]

This would display in a console screen as 100.

The next letter we we'll handel is C, this letter stands for currency, also known as a money symbol, and also if you use this argument, it will seperate the thousands with commas, wich is good when writeing applications that have to do with money. Also, the value outcome is followed by two decimal spaces.

[code]
using System;

namespace FancyFormattingC
{
class CurrencyFormat
{

Static void Main()
{
int iA 25000;
Console.Write(iA.ToString("C"));
}
}
}
[/code]

So this would display as $25,000.00 in the console screen

Lets move on to E, Exponential, this will make a numeric value display in Scientific notation. Scientific notation is used alot in scientific and engineering applications. Here is your code example:

[code]
using System;

namespace FancyFormattingE
{
class Exponential
{

Static void Main()
{
int iA = 1231;
Console.Write(iA.ToString("E");
}
}
}
[/code]

This would display in scientific notaion as 1.231000E+003 in the console screen.

Lets move on to F, FixedPoint, this will make a scientific notation value display as a whole number.This is used to Convert Scientific notaion value into a whole number(usaly) I'm going to show a example of a reverse of the prior example above.

[code]
using System;

namespace FancyFormattingF
{
class FixedPoint
{

Static void Main()
{
float fA = 1.231000E+003;
Console.Write(fA.ToString("F"));
}
}
}
//Usaly you would not declare a variable and turn right  around and resign it a differnt data type
[/code]

This would display 1231.00 in the console screen.

Lets move on to N, "Number", This will seperate the thousands with commas, this is another good feature to use when dealing with money applications, Another thing you must remember is to put the quatations around these arguments. examp: iA.ToString("N");
So lets write some code for this...

[code]
using System;

namespace FancyFormattingN
{
class Number
{

Static void Main()
{
int nA = 123321456;
Console.Write(nA.ToString("N"));
}
}
}
[/code]

This would display as 123,321,456.00 in the console scren. //This is very similar to F

Now were going to move onto "P", Percent, In this case of sample code, we are converting a decimal into a Percent value, This is often used in Population applications. What actualy happens, is, the value this is applying to, is multiplied by 100, a percent sign is added, and the thousands are seperated by commas.

[code]
using System;

namespace FancyFormattingP
{
class Percent
{

Static void Main()
{
decimal dA = 0.234m; /*Remeber to add the m when using decimal data types.*/
Console.Write(dA.ToString("P"));
}
}
}
[/code]

This would display 23.40 in the console screen

The next argument we will handel is X, Hexadecimal, This only works with integers, it will convert a whole number into a Hexadecimal value type, This is a very, very useful you if you need to display hexadecimal as a string in the console. This only works with integers

[code]
using System;

namespace FancyFormattingX
{
class Hexidecimal
{

Static void Main()
{
int xA = 1222;
Console.Write(xA.ToString("X"));
}
}
}
[/code]

So this would display as 4C6 in the console screen.



Fin
November 19, 2004, 4:51 AM
Myndfyr
Couple questions:

1.) Why are you using Hungarian notation when you're using a strongly-typed language?  I tend to use some more traditional items, such as "n" as a prefix for a number, "sz" for strings, and "p" for any pointer-types.  But that's about it.
2.) Might be a good idea to include something about String String.Format(params string[]); --
For any of your examples, you can replace them with:
[code]String.Format("My number will display right over here: {0}", result);[/code]
I like to use that particularly for formatting in hex.  You can append :code-fixed-digits to the number:
[code]String.Format("My hex number is: {0:x2}", result);[/code]
The above code displays at the very least two digits of the result in hex.
That works similarly for the ToString function.
November 19, 2004, 7:41 AM
DecA
1 - I have learned to use hungarian notation in large projects where variable data types can get confusing, even though it wasn't intended for that.

2 - I didn't want to confuse people who are new to C#, i wanted to keep this as simple as possible, and let them get the hang of it, then in my follow up lesson,  wich im having doubts on now, i was gonna go into more advanced features.
November 19, 2004, 9:30 AM
Zakath
Myndfyre, great minds think alike!

[quote][00:30:28] Zackath: just out of curiosity...isn't C# strongly typed?
[00:30:57] Deca: yes
[00:31:00] Zackath: uh
[00:31:06] Zackath: so why do you need hungarian notation?
[00:31:26] Deca: for teh same reason you would with any lang.
[00:31:29] Zackath: no...
[00:31:41] Zackath: the reason it was used in C was because C is weakly typed[/quote]
November 19, 2004, 3:54 PM
DecA
[color=Pink]Err, you should put the rest of our convo. :P You made it look i lost when the fact is, that i won :P hahaha ;D[/color]

[color=LimeGreen]And for the last time, I used hungarian notation to better identify variable data types!![/color]
November 19, 2004, 4:02 PM
Myndfyr
[quote author=DecA link=topic=9605.msg89373#msg89373 date=1100880159]
[color=Pink]Err, you should put the rest of our convo. :P You made it look i lost when the fact is, that i won :P hahaha ;D[/color]

[color=LimeGreen]And for the last time, I used hungarian notation to better identify variable data types!![/color]
[/quote]

It's used to identify variable data types in a language in which no other mechanism exists to do so.  C# is strongly typed, and your IDE (unless it's notepad, which isn't an IDE) lets you identify the type of variable anyway.  It's moot.
November 19, 2004, 4:52 PM
DecA
But if you come to say C#, from say C/C++ you would already be very familiar with hungarian notaion, so why not just use a already good working method of identifying variable data types, rather then make your own up, and what if you are working on a group project, and people who try and do working with your code don't understand your way of identifying..anyways, im not replying to this anymore......lol :-P  Unless someone has a qustion.
November 19, 2004, 10:08 PM
Maddox
[quote author=DecA link=topic=9605.msg89407#msg89407 date=1100902129]
But if you come to say C#, from say C/C++ you would already be very familiar with hungarian notaion, so why not just use a already good working method of identifying variable data types, rather then make your own up, and what if you are working on a group project, and people who try and do working with your code don't understand your way of identifying..anyways, im not replying to this anymore......lol :-P  Unless someone has a qustion.
[/quote]

Hungarian notation is just another method of Microsoft to make you servile. Free your mind.
November 20, 2004, 7:04 AM
DecA
To view the second lesson goto:
https://davnit.net/bnet/vL/phpbbs/index.php?topic=9606.0
November 20, 2004, 7:56 AM
Arta
Also, don't the CLR coding guidelines specifically say that you shouldn't use hungarian for .NET projects?
December 1, 2004, 5:34 AM
Myndfyr
[quote author=Arta[vL] link=topic=9605.msg90672#msg90672 date=1101879264]
Also, don't the CLR coding guidelines specifically say that you shouldn't use hungarian for .NET projects?
[/quote]

I can't imagine that they would say anything about how private members are named.
December 1, 2004, 6:39 AM

Search