String formatting problems
-
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
-
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
hi, if you don't mind please give little bit specific. ************************** S r e e j i t h N a i r **************************
-
hi, if you don't mind please give little bit specific. ************************** S r e e j i t h N a i r **************************
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
-
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
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 **************************
-
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 **************************
-
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 :)