Forcing positive sign on double in .Net String.Format
        Posted  
        
            by Max Yaffe
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Max Yaffe
        
        
        
        Published on 2010-04-06T21:47:35Z
        Indexed on 
            2010/04/06
            22:43 UTC
        
        
        Read the original article
        Hit count: 300
        
Context: .Net, C#
I want to print a complex number made from two doubles. The sign needs to show on the imaginary part. I'd like to use the default double formatting for each part to minimize the number of characters.
I tried using String.Format("{0:+G;-G}{1:+G;-G}j", real, imaginary) but this ended up printing: "+G-Gj". Not quite what I wanted.
Is there any way to do this using the G specifier or do I need to do a custom format which would sacrifice auto-switching the exponent, e.g. {1:+#.######e###;-#.######e###}j"
© Stack Overflow or respective owner