Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Convert to string with a [+] or a [-] sign symbol

Convert to string with a [+] or a [-] sign symbol

Scheduled Pinned Locked Moved C#
helptutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    student_rhr
    wrote on last edited by
    #1

    sign symbol for example I have: double x = 500.12; double y = -500.12; I want x to be diplayed as: +500.12 and y to be displayed as: -500.12 Please help

    A P 2 Replies Last reply
    0
    • S student_rhr

      sign symbol for example I have: double x = 500.12; double y = -500.12; I want x to be diplayed as: +500.12 and y to be displayed as: -500.12 Please help

      A Offline
      A Offline
      Anthony Mushrow
      wrote on last edited by
      #2

      The negative number should come out with a - sign already. So once you've used .ToString, if the first char isn't a '-' then put a '+' at the start:

      double x = 500.12;
      double y = -500.12;

      string sX;
      string sY;

      sX = x.ToString();

      if(!sX.StartsWith("-"))
      sX = "+" + sX;

      sY = y.ToString();

      if(!sY.StartsWith("-"))
      sY = "+" + sY;

      Console.WriteLine(sX + "\n" + sY);

      The output should be: +500.12 -500.12

      My current favourite word is: PIE! Good ol' pie, it's been a while.

      1 Reply Last reply
      0
      • S student_rhr

        sign symbol for example I have: double x = 500.12; double y = -500.12; I want x to be diplayed as: +500.12 and y to be displayed as: -500.12 Please help

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Thanks for the opportunity to look into this.:-D

        System.Console.WriteLine ( ( 500.12).ToString("+#.##;-#.##" ) ) ;
        System.Console.WriteLine ( (-500.12).ToString("+#.##;-#.##" ) ) ;

        S 1 Reply Last reply
        0
        • P PIEBALDconsult

          Thanks for the opportunity to look into this.:-D

          System.Console.WriteLine ( ( 500.12).ToString("+#.##;-#.##" ) ) ;
          System.Console.WriteLine ( (-500.12).ToString("+#.##;-#.##" ) ) ;

          S Offline
          S Offline
          student_rhr
          wrote on last edited by
          #4

          Works like a charm. Thanks.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups