|
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
|
|
|
|
|
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
|
|
|
|
|
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...
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.
|
|
|
|
|
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
|
|
|
|
|
jkpieters wrote: Why doesn't this work as expected?
Because it's a string rather than a number?
jkpieters wrote: Other solutions?
0) Don't have it as a string (parse it if necessary ).
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.
|
|
|
|
|
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));
|
|
|
|
|
I hope you don't get any negative numbers...
|
|
|
|
|
They're strings, not numbers.
|
|
|
|
|
even numeric strings could have a negative inclination.
|
|
|
|
|
Luc Pattyn wrote: numeric strings
Oxymoron.
Luc Pattyn wrote: negative inclination
I decline to respond to that.
|
|
|
|
|
whats wrong with this, why cant i use a textbox to write a saveToFolder?
wc.DownloadFileAsync(new Uri(sFiles.Trim()), String.Format(textBoxSavetoFolder.Text, "{0}.xml", ("omgang=") +i, Path.GetExtension(sFiles)));
with this below it works
wc.DownloadFileAsync(new Uri(sFiles.Trim()), String.Format(@"c:\Users\KMJ\test\{0}.xml", +i, Path.GetExtension(sFiles)));
|
|
|
|
|
try this:
wc.DownloadFileAsync(new Uri(sFiles.Trim()), String.Format(textBoxSavetoFolder.Text+ "{0}.xml", ("omgang=") +i, Path.GetExtension(sFiles)));
EASY COME EASY GO
|
|
|
|
|
omg, that wasnt so hard.
Thank you very much
|
|
|
|
|
Hi,
A quote from MSDN about this event: "This event occurs whenever cells are selected or the selection is canceled, whether programmatically or by user action".
My question: How can I get the exact same functionality, but only as a result of user action (I do not wish this even to occur when the selection has programmatically changed).
Thanks,
Eyal.
|
|
|
|
|
What I can think of is to have a flag (boolean variable) in your form. Now, when you are changing selected cell through code, set this to false and make it true again in the end of event. Something like this:
bool isChangedByUser = true;
.
.
.
isChangedByUser = false;
.
.
.
void dgv_SelectionChanged(object sender, EventArgs e)
{
if(isChangedByUser)
{
}
isChangedByUser = true;
}
|
|
|
|
|
Thank for the reply.
The problem is not that I'm changing the selection - in this case the problem is easy to solve. The problem is that the event is fired when the form is initially opened and rows are automatically added. Then, when the first row is (automatically) selected, the event is fired.
Eyal.
|
|
|
|
|
Following are two of the various ways you can do this:
1. Attach the event handler once you are done with filling the DataGridView with data.
2. Have a boolean variable and use it to decide if the code inside the event handler should be executed or not.
|
|
|
|
|
Hi,
i want to hide cmd prompt while executing or running an .bat file..
i have an exe inside my .bat file..
Simply want to run .exe without showing the command prompt through bat file..
Regards,
Pawan
|
|
|
|
|
How is this related to C#?
|
|
|
|
|
Hi,
StreamWriter sw = new StreamWriter(@"Z:\" + txtName.Text + ".bat");
sw.WriteLine("START /d E:\\Stock\\StockView\\bin\\Debug Stock.exe % "+result);
sw.Close();
This is what i created through C# code..
I don't know what i need to include before or after "START" of sw.WriteLine to hide the cmd prompt..
That's y i posted it in C#.
|
|
|
|
|
Use @echo off on the first line of your bat file.
|
|
|
|