Not so complex numbers in C#
Not so complex numbers in C#
Written by Mike James   
Tuesday, 25 March 2014
Article Index
Not so complex numbers in C#
Complex functions

Banner

Formatting

One last topic - formatting complex numbers.

The ToString method has a number of overloaded versions that can be used to apply a custom format. As we have already seen the simple ToString() method returns a string formatted as (real,imaginary).

The ToString(string) method will apply a standard format string to the two double precision values that make up the number. You can look up "Standard Numeric Format Strings" in the documentation.  For example:

Complex z = Complex.Exp(
             Complex.ImaginaryOne * Math.PI);
Console.WriteLine(z.ToString("F3"));

outputs:

(-1.000, 0.000)

That is two fixed point values with 3 digits after the decimal point. You can only use format specifiers that apply to Double.

The methods ToString(IFormatProvider) and ToString(string,IFormatProvider) will format the number using the culture specified by the IFormatProvider and in the second case will also format the doubles using the format string.

For example:

Console.WriteLine(
       z.ToString( new CultureInfo("es-ES")));
Console.WriteLine(
       z.ToString("F3",new CultureInfo("es-ES")));

displays:

(-1, 1,22460635382238E-16)
(-1,000, 0,000)

Note: es-ES is the specifier for Spanish and Spain.

The documentation contains an example of building a custom formatter that can be used to print values in the form a+ib or a+jb. However because you can't inherit from a struct  and even adding extension methods is difficult trying to extend Complex in any reasonably object-oriented way is doomed to fail.

csharp

 

If you would like to see an example of Complex in use then see:

Mandelbrot Zoomer in WPF

Related Articles

Mandelbrot Zoomer in WPF

The Programmer's Guide to Fractals       

How To Draw Einstein's Face Parametrically       

Good Math       

MathJS A Math Library For JavaScript       

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

 
 
 

blog comments powered by Disqus

 

Banner


Deep C# - Anonymous Methods, Lambdas And Closures

Anonymous methods aren't particularly new, but they have hidden depths and lead on to lambdas and the idea of a closure. These are all important ideas in modern programming.



Multicast Delegates and Events

Multicast delegates are useful in their own right but they also form the basis on which the C# event system is built. We take a close look at how they work and how to use them. For example, did you kn [ ... ]


Other Articles

 

<ASIN:0387257683>

<ASIN:0521706858>

<ASIN:0321637135>

<ASIN:0521880688>

<ASIN:818561816X>

<ASIN:3540739157>

<ASIN:0954612078>



Last Updated ( Tuesday, 25 March 2014 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2016 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.