• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Formatting numbers

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Formatting numbers

    Applies to NinjaTrader 8 and NinjaTrader 7

    String formatting on numbers is very useful for creating readable output. This can be done through the use of the number object’s ToString() method.

    A common practice is printing out mathematical operations with the use of the ToString() method on the double object. What usually happens is the printing of a long string containing all the decimal places existing in the double. This sometimes makes output cluttered and hard to read. Luckily, C# has a robust set of string formatting options available to make the string more comprehendible.

    Here is a list of common formatting options available in the ToString() method:
    Code:
    double c = 10.25693;
    Print("No formatting: " + c.ToString());
    Print("Currency formatting: " + c.ToString("C"));
    Print("Exponential formatting: " + c.ToString("E"));
    Print("Fixed-point formatting: " + c.ToString("F2"));
    Print("General formatting: " + c.ToString("G"));
    Print("Percent formatting: " + c.ToString("P0"));
    Print("Formatted to 2 decimal places: " + c.ToString("N2"));
    Print("Formatted to 3 decimal places: " + c.ToString("N3"));
    Print("Formatted to 4 decimal places: " + c.ToString("N4"));
    The corresponding output is as follows:
    Code:
    No formatting: 10.25693
    Currency formatting: $10.26
    Exponential formatting: 1.025693E+001
    Fixed-point formatting: 10.26
    General formatting: 10.25693
    Percent formatting: 1,026 %
    Formatted to 2 decimal places: 10.26
    Formatted to 3 decimal places: 10.257
    Formatted to 4 decimal places: 10.2569
    For custom formatting you can use the following:
    Code:
    double phoneNumber = 9162031022;
    Print("Phone number: " + phoneNumber.ToString("(###) ### - ####"));
    Corresponding output:
    Code:
    Phone number: (916) 203 - 1022
    For more information on general string formatting this guide may be of use. Many other resources can be found online through a google search as well.
    http://radio.weblogs.com/0106479/sto...attingInC.html
    Last edited by NinjaTrader_Jesse; 06-03-2015, 12:43 PM.
    JoshNinjaTrader Customer Service

Latest Posts

Collapse

Topics Statistics Last Post
Started by dmackay41, Today, 12:38 AM
2 responses
5 views
0 likes
Last Post dmackay41  
Started by cwolters, Today, 12:18 AM
0 responses
2 views
0 likes
Last Post cwolters  
Started by trdmark, Today, 12:13 AM
0 responses
8 views
0 likes
Last Post trdmark
by trdmark
 
Started by istock999, Yesterday, 10:14 PM
0 responses
0 views
0 likes
Last Post istock999  
Started by nepenthean, Yesterday, 10:06 PM
1 response
3 views
0 likes
Last Post NinjaTrader_EricB  
Working...
X