This question already has answers here:
String format currency
(8 answers)
Closed 8 years ago.
I need to format my display tostring results to a currency in C#
display = "Service Amount: " + service + "<br>" +
"Discount Amount: " + discountAmount + "<br>" +
"Total: " + total + "<br>";
lblDisplay.Text = display;
I did try the following:
display = "Service Amount: " + Console.Write(int.ToString("c",service)) + "
but I couldn't figure out what variables to put in. I just need it to display as $35.00 after the it returns the string.
Try:
display = string.Format("Service Amount: {0}",service.ToString("C"));
Console.WriteLine(display);
You can also look at StringBuilder for building strings or String.Format
String.Format("Service Amount: {0:C}<br>", service)
Related
This question already has answers here:
Binding Listbox to List<object> in WinForms
(8 answers)
Closed 3 years ago.
I write a program, it is necessary to switch the current status in it, as well as it is necessary to plan it when you plan an event, it is perceived as an object, the object has its own fields, such as the start time and end time of the event, I want this object to be output when generated sheet boxing.
Tell me how can this be done?
List<ChangeStatus> events = new List<ChangeStatus>();
private void toPlanButton_Click(object sender, EventArgs e)
{
string comboBoxTypeNumber = comboBoxType.SelectedItem.ToString();
DateTime Time = new DateTime();
Time = dateTimePicker1.Value;
DateTime longTime = new DateTime();
longTime = dateTimePicker2.Value;
ChangeStatus statusEvent = new ChangeStatus();
statusEvent.StartEvent = Time;
statusEvent.LongEvent = longTime;
statusEvent.TypeEvent = comboBoxTypeNumber;
events.Add(statusEvent);
TimeComparer tc = new TimeComparer();
events.Sort(tc);
}
How to display an object in listbox?
It is necessary to display a list of objects, because in the future I want to make editing objects
listBoxEvent.Items.Add("type: " + statusEvent.TypeEvent + ";" + " start: " + statusEvent.StartEvent + ";" + " long: " + statusEvent.LongEvent + " min;"); - work
You can use System.Linq Linq to get the string text and can call the AddRange() method on Items collection like
List<string> listData = events.Select(x => "type: " + x.TypeEvent + ";" + " start: " + x.StartEvent + ";" + " long: " + x.LongEvent + " min;").ToList();
listBoxEvent.DataSource = listData;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am using this in my controller:
char[] arrDate = date.ToArray();
DateTime dt = DateTime.Parse(arrDate[0] + arrDate[1] + "/" +
arrDate[2] + arrDate[3] + "/" +
arrDate[4] + arrDate[5] + arrDate[6] + arrDate[7]);
The error:
System.FormatException: String was not recognized as a valid DateTime.
Consider this:
var date = "11252017";
var arrDate = date.ToArray();
var strDate = arrDate[0] + arrDate[1] + "/" +
arrDate[2] + arrDate[3] + "/" +
arrDate[4] + arrDate[5] + arrDate[6] + arrDate[7]; // 98/25/2017
Notice that:
'1' + '1' = 98* ⇒ char + char = int
98 + "/" = "98/" ⇒ int + string = string
"98/" + '2' = "98/2" ⇒ string + char = string
The fix:
var dt = DateTime.Parse("" +
arrDate[0] + arrDate[1] + "/" +
arrDate[2] + arrDate[3] + "/" +
arrDate[4] + arrDate[5] + arrDate[6] + arrDate[7]);
*ASCII representation:
'1' in decimal is 49
I assume date is of type string. For parsing a string the DateTime class has several methods of which ParseExact is one. This method can parse a string given a format specifier and a culture. In your case the date can be parsed like this:
var date = "11252017";
var dt = DateTime.ParseExact(date, "MMddyyyy", CultureInfo.InvariantCulture);
By the way a string is an array of chars, so in your code arrDate[0] is exactly the same as date[0]. Just something to keep in mind for the future.
This question already has answers here:
Escape double quotes in a string
(9 answers)
How can I add double quotes to a string that is inside a variable?
(20 answers)
Closed 7 years ago.
I have been looking at this for a while trying everything I can find on SO. There are many posts but I must be missing something.
I am building a string and need to get a double quote in it between two variables.
I want the string to be
convert -density 150 "L:\03 Supervisors\
The code is
tw.WriteLine(strPrefix + " " + strLastPath);
where
strPrefix = convert -density 150
and
strLastPath = L:\\03 Supervisors\
I have tried many combinations of "" """ """" \" "\ in the middle where the space is being inserted.
Please show me what I am missing.
You have two options:
var output1 = strPrefix + "\"" + strLastPath;
Or using a verbatim string:
var output2 = strPrefix + #"""" + strLastPath;
Here's a sample console application that achieves what you're after:
namespace DoubleQuote
{
class Program
{
static void Main(string[] args)
{
var strPrefix = "convert - density 150";
var strLastPath = #"L:\\03 Supervisors\";
Console.WriteLine(strPrefix + " \"" + strLastPath);
Console.ReadKey();
}
}
}
If written as a format string it would look like this:
var textToOutput = string.Format("{0} \"{1}", strPrefix, strLastPath);
Console.WriteLine(textToOutput);
Please try this
var strPrefix = "convert -density 150";
var strLastPath = #"L:\03 Supervisors\";
Console.WriteLine(strPrefix + " " + '"'+strLastPath);
I'am exporting some data to a .txt file as follows:
String content;
String path=#"e:\coding\";
String name="test.txt";
path+=name;
System.IO.File.Delete(path);
for (i=0;i<row-1;i++)
{
try
{
if (r[i].points.Count() > 2)
{
content = "Route " + (i + 1).ToString() +" Truck_id:"+trk[i].truck_name.ToString()+ " Max_load="+trk[i].capacity.ToString()+ "\n";
System.IO.File.AppendAllText(path, content + Environment.NewLine);
System.IO.File.AppendAllText(path, "Points Load Reached_AT Max_load" + Environment.NewLine);
System.IO.File.AppendAllText(path, "========================================" + Environment.NewLine);
for (int j = 0; j < (r[i].points.Count()); j++)
{
content = r[i].points[j].ToString() + " " + c[r[i].points[j]].load.ToString() +" "+ r[i].time_list[j].ToString()+" "+c[r[i].points[j]].max_load.ToString()+"\n";
System.IO.File.AppendAllText(path, content + Environment.NewLine);
}
content = "Total " + r[i].ld.ToString() + "\n";
System.IO.File.AppendAllText(path, content + Environment.NewLine );
content = "Route Complete: " + r[i].reach_at.ToString();
System.IO.File.AppendAllText(path, content + Environment.NewLine+Environment.NewLine);
}
}
catch (IndexOutOfRangeException e)
{
break;
}
}
As expected the output I get is not properly formatted:
The spaces are causing the text to be jumbled and not arranged. My reputation does'nt allow me to post a screenshot but I guess It can be understood what is happening.
Is there way so that the text is properly formatted neatly column wise without looking jumbled.
If you need a text, you can use tabs:
System.IO.File.AppendAllText(path, "Points\t\tLoad\t\tReached_AT\t\tMax_load" + Environment.NewLine);
// ...
content = r[i].points[j].ToString() + "\t\t " + c[r[i].points[j]].load.ToString() +"\t\t"+ r[i].time_list[j].ToString()+"\t\t"+c[r[i].points[j]].max_load.ToString()+"\n";
Just play with amount of tabs (\t for one, \t\t for two, etc...). Hope it can help.
Another solution would be to use commas:
System.IO.File.AppendAllText(path, "Points,Load,Reached_AT,Max_load" + Environment.NewLine);
and save to CSV-file (comma-separated values). Then you can import the data to Microsoft Excel or to other software.
You can find bunch full of good information on how to format the string contents in the The format item MSDN but for quick answer, an example for your string
content = "Route " + (i + 1).ToString() + " Truck_id:" + trk[i].truck_name.ToString() + " Max_load=" + trk[i].capacity.ToString() + "\n";
If we assume,
i maximum 10 digits,
Truck_name max 45 characters
capacity max 10 digits
content = String.Format("{0,-20}{1,55}{2,20} " + Environment.NewLine, "Route " + (i + 1).ToString(), " Truck_id:" + trk[i].truck_name.ToString(), " Max_load=" + trk[i].capacity.ToString());
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
strFirstName = txtFirstName.Text;
strLastName = txtLastName.Text;
lblSummary = "FirstName:" + strFirstName + Environment.NewLine +
"Last Name:" + strLastName + Environment.NewLine +
"Gross Income:" + decGrossIncome.ToString("c") + Environment.NewLine +
"Taxes Due:" + decTaxesDue.ToString("c") + Environment.NewLine +
"Total Payments:" + decTotalPayments.ToString("c") + Environment.NewLine +
"Total Amount Due:" + decTotalAmountDue.ToString("c");
lblSummary.Text = strSummary;
*note*the following contains the error
"Total Amount Due:" + decTotalAmountDue.ToString("c");
The error I get is:
Error 1 Cannot implicitly convert type 'string' to 'System.Windows.Forms.Label' E:\CIS 162 AD\CS03\CS03\CS03\Form1.cs 79 37 CS03
You're trying to assign a string value straight to a Label variable. I suspect you meant this:
string strSummary = "FirstName:" + strFirstName + Environment.NewLine +
"Last Name:" + strLastName + Environment.NewLine +
"Gross Income:" + decGrossIncome.ToString("c") + Environment.NewLine +
"Taxes Due:" + decTaxesDue.ToString("c") + Environment.NewLine +
"Total Payments:" + decTotalPayments.ToString("c") + Environment.NewLine +
"Total Amount Due:" + decTotalAmountDue.ToString("c");
lblSummary.Text = strSummary;
I would personally advise using string.Format, and ditching the pseudo-Hungarian naming by the way.
string summary = string.Format("First Name: {1}{0}" +
"Last Name: {2}{0}" +
"Gross Income: {3:c}{0}" +
"Taxes Due: {4:c}{0}" +
"Total Payments: {5:c}{0}" +
"Total Amount Due: {6:c}",
Environment.NewLine, firstName, lastName,
grossIncome, taxesDue, totalPayments,
totalAmountDue);
// I'm not so hot on naming controls, so I'm not saying this is great - but I
// prefer it not to be control-type-specific; what's important is that it's a
// control we're using to output the summary.
summaryOutput.Text = summary;