How to pad a string with zeroes
-
Hi, I have the following string "455.56" I want this to format in: 00455.5600. I tried this and was expecting it to work fine: string strLong = String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", "455.66"); This statement results in: "455.66"!! Why doesn't this work as expected? Other solutions? thanks for your time. Regards, Jan
-
Hi, I have the following string "455.56" I want this to format in: 00455.5600. I tried this and was expecting it to work fine: string strLong = String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", "455.66"); This statement results in: "455.66"!! Why doesn't this work as expected? Other solutions? thanks for your time. Regards, Jan
Try feeding it a number rather than a string...
string strLong = String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", 455.66);
The format info :00000.0000 is for numeric formats only: MSDN String.Format formats[^] [edit]Should have explained the format is numeric... Oops![/edit]
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
Try feeding it a number rather than a string...
string strLong = String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", 455.66);
The format info :00000.0000 is for numeric formats only: MSDN String.Format formats[^] [edit]Should have explained the format is numeric... Oops![/edit]
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
Thanks for the answer, however I did fed it a number also like this String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", 455.66); But the point is that the data to be formatted is in a string[] and even when I convert the string first to a double or whatever the result is the same, even the conversion is giving funky results: 45566 instead of 455.66!! I am not sure but I must be doing something stupid probably.
-
Hi, I have the following string "455.56" I want this to format in: 00455.5600. I tried this and was expecting it to work fine: string strLong = String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", "455.66"); This statement results in: "455.66"!! Why doesn't this work as expected? Other solutions? thanks for your time. Regards, Jan
jkpieters wrote:
Why doesn't this work as expected?
Because it's a string rather than a number?
jkpieters wrote:
Other solutions?
- Don't have it as a string (parse it if necessary X| ). 1) Use PadLeft and PadRight. 2) Perhaps use my ApplyFormat[^].
System.Console.WriteLine ( "455.56".ApplyFormat ( "/10,10,0:" ) ) ;
But getting the decimal points to line up might be tricky in some cases.
- Don't have it as a string (parse it if necessary X| ). 1) Use PadLeft and PadRight. 2) Perhaps use my ApplyFormat[^].
-
Thanks for the answer, however I did fed it a number also like this String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", 455.66); But the point is that the data to be formatted is in a string[] and even when I convert the string first to a double or whatever the result is the same, even the conversion is giving funky results: 45566 instead of 455.66!! I am not sure but I must be doing something stupid probably.
To do this while keeping it as a string will be messy:
string x = "456.78"; string\[\] parts = x.Split('.'); string whole = parts\[0\]; string decpart = parts\[1\]; string y = new string('0', 5 - whole.Length) + whole; string z = decpart + new string('0', 4 - decpart.Length); string padded = y + "." + z;
And that has no error checking! To convert to double and back is probably the best way:
string x = "456.78"; double d = double.Parse(x, System.Globalization.CultureInfo.InvariantCulture); string padded = string.Format("{0:00000.0000}", d);
But you will have to be absolutely sure that your number strings are all in "nnn.nn" format - remember that some cultures use "nnn,nn" which would bolox you right up! (That may be why your conversion gave funky results...)
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
Hi, I have the following string "455.56" I want this to format in: 00455.5600. I tried this and was expecting it to work fine: string strLong = String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", "455.66"); This statement results in: "455.66"!! Why doesn't this work as expected? Other solutions? thanks for your time. Regards, Jan
Not sure if this is what you mean but try this: string strLong = String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", Double.Parse("455.66", CultureInfo.InvariantCulture));
-
To do this while keeping it as a string will be messy:
string x = "456.78"; string\[\] parts = x.Split('.'); string whole = parts\[0\]; string decpart = parts\[1\]; string y = new string('0', 5 - whole.Length) + whole; string z = decpart + new string('0', 4 - decpart.Length); string padded = y + "." + z;
And that has no error checking! To convert to double and back is probably the best way:
string x = "456.78"; double d = double.Parse(x, System.Globalization.CultureInfo.InvariantCulture); string padded = string.Format("{0:00000.0000}", d);
But you will have to be absolutely sure that your number strings are all in "nnn.nn" format - remember that some cultures use "nnn,nn" which would bolox you right up! (That may be why your conversion gave funky results...)
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
I'd use a Regular Expression to perform the split.
-
I'd use a Regular Expression to perform the split.
I thought of doing it via Linq to SQL and a stored procedure, but decided it was too much typing... :laugh:
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
I thought of doing it via Linq to SQL and a stored procedure, but decided it was too much typing... :laugh:
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
I would think your small intestine would leap up your throat and throttle your brain if you tried that.
-
I would think your small intestine would leap up your throat and throttle your brain if you tried that.
Yeah, but it'd be worth it - I could post it to "Coding Horrors"
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
Hi, I have the following string "455.56" I want this to format in: 00455.5600. I tried this and was expecting it to work fine: string strLong = String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", "455.66"); This statement results in: "455.66"!! Why doesn't this work as expected? Other solutions? thanks for your time. Regards, Jan
I hope you don't get any negative numbers... :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
I hope you don't get any negative numbers... :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
They're strings, not numbers. :-D
-
They're strings, not numbers. :-D
even numeric strings could have a negative inclination. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
even numeric strings could have a negative inclination. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
Luc Pattyn wrote:
numeric strings
Oxymoron. :-D
Luc Pattyn wrote:
negative inclination
I decline to respond to that.