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. String formatting problems

String formatting problems

Scheduled Pinned Locked Moved C#
csharptutorialquestioncareer
6 Posts 2 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.
  • M Offline
    M Offline
    moredip
    wrote on last edited by
    #1

    Hi all, I'm writing some c# code to output some data in a format that a Fortran program can use. Yep, that's right, I did say Fortran :) What I need is to write a floating point number into a string that is always 7 characters long, with a space prefixed before the number. For example:  1.34567  1234.67  .234567 I figured that this would be a simple job with String.Format(), like so: double dNumber; String.Format(" {0,7:g}", dNumber ); however, the output of this is something like:  91.3481478886182  141.785454332755 it seems that the alignment part of the format string is being ignored. Can anyone tell me what I'm missing here? Cheers, Pete

    S 1 Reply Last reply
    0
    • M moredip

      Hi all, I'm writing some c# code to output some data in a format that a Fortran program can use. Yep, that's right, I did say Fortran :) What I need is to write a floating point number into a string that is always 7 characters long, with a space prefixed before the number. For example:  1.34567  1234.67  .234567 I figured that this would be a simple job with String.Format(), like so: double dNumber; String.Format(" {0,7:g}", dNumber ); however, the output of this is something like:  91.3481478886182  141.785454332755 it seems that the alignment part of the format string is being ignored. Can anyone tell me what I'm missing here? Cheers, Pete

      S Offline
      S Offline
      sreejith ss nair
      wrote on last edited by
      #2

      hi, if you don't mind please give little bit specific. ************************** S r e e j i t h N a i r **************************

      M 1 Reply Last reply
      0
      • S sreejith ss nair

        hi, if you don't mind please give little bit specific. ************************** S r e e j i t h N a i r **************************

        M Offline
        M Offline
        moredip
        wrote on last edited by
        #3

        Umm, I'm not sure how much more specific I can be, but I'll try :) Ok, I have a variable of type double, let's call it dNumber. I want to create a string that represents that number. Let's call the string strFormatted. The string must be formatted as follows: the 1st character is a space the next 7 characters represent the number the string must always be 8 characters in length here's some examples of correctly formatted strings. 12345678 <- characters in string --------  1.34567  1234.67  0.23456 I figured the following function would do the trick: string FormatToFortran( double dNumber ) { return String.Format( " {0,7:g}", dNumber ); } but unfortunately it doesn't. Format() seems to be ignoring the alignment section of the format string (the 7), and strings returned by FormatToFortran() are similar to the following:  130.312393712128  136.399493519436  121.295216530426 obviously, these aren't 8 characters in length. So, I'm wondering what is wrong with my format string. Hope that clarifies things. If not, feel free to ask away! Cheers, Pete

        S 1 Reply Last reply
        0
        • M moredip

          Umm, I'm not sure how much more specific I can be, but I'll try :) Ok, I have a variable of type double, let's call it dNumber. I want to create a string that represents that number. Let's call the string strFormatted. The string must be formatted as follows: the 1st character is a space the next 7 characters represent the number the string must always be 8 characters in length here's some examples of correctly formatted strings. 12345678 <- characters in string --------  1.34567  1234.67  0.23456 I figured the following function would do the trick: string FormatToFortran( double dNumber ) { return String.Format( " {0,7:g}", dNumber ); } but unfortunately it doesn't. Format() seems to be ignoring the alignment section of the format string (the 7), and strings returned by FormatToFortran() are similar to the following:  130.312393712128  136.399493519436  121.295216530426 obviously, these aren't 8 characters in length. So, I'm wondering what is wrong with my format string. Hope that clarifies things. If not, feel free to ask away! Cheers, Pete

          S Offline
          S Offline
          sreejith ss nair
          wrote on last edited by
          #4

          hi, What i thought is correct. I got correct result.For me it's working properly. i don't know why it is showing like that in your machine.:eek: ************************** S r e e j i t h N a i r **************************

          M 1 Reply Last reply
          0
          • S sreejith ss nair

            hi, What i thought is correct. I got correct result.For me it's working properly. i don't know why it is showing like that in your machine.:eek: ************************** S r e e j i t h N a i r **************************

            M Offline
            M Offline
            moredip
            wrote on last edited by
            #5

            Argh! Really? I'm using the VS 2005 beta. Maybe this is a bug in the .NET 2.0 runtime. Boo. Just to double check, when you call FormatToFortran(), it /always/ returns a string of length 8, no matter what you put in? Thanks for the help, by the way :)

            M 1 Reply Last reply
            0
            • M moredip

              Argh! Really? I'm using the VS 2005 beta. Maybe this is a bug in the .NET 2.0 runtime. Boo. Just to double check, when you call FormatToFortran(), it /always/ returns a string of length 8, no matter what you put in? Thanks for the help, by the way :)

              M Offline
              M Offline
              moredip
              wrote on last edited by
              #6

              OK, well I guess I managed to fix the problem by using the following format string: String.Format(" {0,7:G6}", dNumber ); strange that the previous format string performed differently on different machines though.

              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