• Welcome to Valhalla Legends Archive.
 

Fancy Formatting C# Lesson #1 of 2

Started by DecA, November 18, 2004, 10:51 PM

Previous topic - Next topic

DecA

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

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.

using System;

namespace FancyFormattingG
{
class GeneralFormat
{

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


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.


using System;

namespace FancyFormattingC
{
class CurrencyFormat
{

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


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:


using System;

namespace FancyFormattingE
{
class Exponential
{

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


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.


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


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...


using System;

namespace FancyFormattingN
{
class Number
{

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


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.


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"));
}
}
}


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


using System;

namespace FancyFormattingX
{
class Hexidecimal
{

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


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



Fin

MyndFyre

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:
String.Format("My number will display right over here: {0}", result);
I like to use that particularly for formatting in hex.  You can append :code-fixed-digits to the number:
String.Format("My hex number is: {0:x2}", result);
The above code displays at the very least two digits of the result in hex.
That works similarly for the ToString function.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

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.

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 from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

DecA

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

And for the last time, I used hungarian notation to better identify variable data types!!

MyndFyre

Quote from: DecA on November 19, 2004, 10:02 AM
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

And for the last time, I used hungarian notation to better identify variable data types!!

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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

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.

Maddox

Quote from: DecA on November 19, 2004, 04:08 PM
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.

Hungarian notation is just another method of Microsoft to make you servile. Free your mind.
asdf.


Arta

Also, don't the CLR coding guidelines specifically say that you shouldn't use hungarian for .NET projects?

MyndFyre

Quote from: Arta[vL] on November 30, 2004, 11:34 PM
Also, don't the CLR coding guidelines specifically say that you shouldn't use hungarian for .NET projects?

I can't imagine that they would say anything about how private members are named.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.