Newbie - need to round field to 2 decimal places

Hello all:

Newbie trying to creating labels and trying to round field to 2 decimals. the below line yields the following result:

Output = this["QTY_TOPICK"].ToString();

When I print the label I get "60.00000". I need to reduce it to "60.00" and am unsure of how code this. I believe this is c#.

When you have a chance please advise.


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
Output = this["QTY_TOPICK"].ToString("#,0.00");

Open in new window

rm103Author Commented:
didn't work. I got the following error:

Cannot convert from 'string' to IE to 'System.IFormatProvider'
So you need to convert to a Decimal first then you can provide the ToString with the format; e.g. -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;

namespace EE_Q28744239
	class Program
		static readonly List<Order> orders = new List<Order>();

		static void Main(string[] args)
			orders.AddRange((from i in Enumerable.Range(0, 10) select new Order() { ID = i, Name = string.Format("Order{0}", i), Quantity = ((i + 5) * 2) }));
			DataTable table = orders.ConvertToDataTable();

			for (int i = 0; i < table.Rows.Count; i++)
				Console.WriteLine("Quantity for {0} is: {1}", table.Rows[i]["Name"], Convert.ToDecimal(table.Rows[i]["Quantity"]).ToString("N2"));

	class Order
		public int ID { get; set; }
		public string Name { get; set; }
		public int Quantity { get; set; }

	static class Extensions
		public static DataTable ConvertToDataTable<T>(this IEnumerable<T> source)
			var properties = TypeDescriptor.GetProperties(typeof(T));
			var table = new DataTable();
			foreach (PropertyDescriptor property in properties)
				table.Columns.Add(property.Name, Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType);

			foreach (T item in source)
				DataRow row = table.NewRow();
				foreach (PropertyDescriptor property in properties)
					row[property.Name] = property.GetValue(item) ?? DBNull.Value;
			return table;

Open in new window

Which produces the following output -Capture.JPG
It should be noted that, the ToString method would also accept:
// Provides the same output as using N2

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.