In my report, I have a tablix. Now I want to show the numeric data in f3 format. I have set text box property of the tablix column to number and with three decimal points.
Now when the data is e.g. 12.120 then it shows me 12.12 instead of 12.120.
And I want to show when the data is like 12 to 12.000.
How to do it?
double d = 12.20;
Console.WriteLine(d.ToString("0.000", new CultureInfo("en-US", false)));
First off, here's your reference material.
You can use those format strings, or the placeholder style.
Examples:
Double value = 12.20D;
String str1 = String.Format("{0:F3}", value);
String str2 = value.ToString("F3");
String str3 = value.ToString("0.000");
To make this work with your TextBox, however, you will need to pass the entered values through a routine that applies said formatting.
Using the TextBox's Validated method is a good choice because it is fired after editing is complete, and not while the user is still typing (like TextChanged would):
private void textBox1_Validated(Object sender, EventArgs e)
{
Double dbl;
if (!String.IsNullOrWhiteSpace(textBox1.Text) &&
Double.TryParse(textBox1.Text, out dbl))
{
//Replace with your formatting of choice...
textBox1.Text = String.Format("{0:F3}", dbl);
}
}
You have to set TextBox.Format property to f3.
Related
I am working on a Page where I have several Entries to enter numerical values (integers and with decimal point).
The requirement is that each field formats the value with decimal point and thousands separators (eg: 1.254.356,42).
The following code formats the value correctly. The problem is that the cursor does jumps to the end always when the text changes:
private void TbLenght_TextChanged(object sender, TextChangedEventArgs e)
{
var textbox = (Entry)sender;
var tempValue = double.Parse(textbox.Text, culture);
var newFormat = tempValue.ToString("N2", culture);
textbox.Text = newFormat;
}
Which options do I have to achieve my goal?
The following code formats the value correctly. The problem is that the cursor does jumps to the end always when the text changes:
You can change current CursorPosition for entry to make current CursorPosition before the decimal point, please take a look the following code:
var textbox = (Entry)sender;
var tempValue = double.Parse(textbox.Text, culture);
var newFormat = tempValue.ToString("N2", culture);
textbox.Text = newFormat;
textbox.CursorPosition = newFormat.Length-3;
If my reply solved your issue, please remember to mark my reply as answer, thanks.
Ok guys, I have a ListBox which displays products (they are a custom class and have ID, name, price) that are in a binding list.. I want the ListBox to display the item name AND the price. The listbox (lbProductsChosen) has "DataTextField" set to the Name and DataValueField set to the ID. I am using the PreRender event to check each Item, look at its price from the binding list (blProducts) etc. and it works great. I get the name and price displayed in the list with this. However, when it is displayed, despite me formatting it using String.Format, the result is still a decimal number (ex. 3.20000) and it just looks ugly. Does anyone know why its working to display it, but not displaying it how I want it formatted.
protected void lbProductsChosen_PreRender(object sender, EventArgs e)
{
foreach (ListItem item in lbProductsChosen.Items)
{
string currentDescription = item.Text;
int convertedValue = Convert.ToInt32(item.Value);
for (int i = 0; i < blProducts.Count; i++)
{
if (blProducts[i].ProductID == convertedValue)
{
decimal ItemPrice = blProducts[i].Price;
string convertedPrice = ItemPrice.ToString();
string currentPrice = String.Format("{0:c}", convertedPrice);
string currentDescriptionPadded = currentDescription.PadRight(30);
item.Text = currentDescriptionPadded + currentPrice;
}
}
}
}
MSDN states the following about the String.Format method.
Generally, objects in the argument list are converted to their string representations by using the conventions of the current culture, which is returned by the CultureInfo.CurrentCulture property.
If you use the currency format specifier c it will use the currency format which is defined in the system settings. Take a look under control panel -> region -> additional settings -> currency. Maybe you have messed up settings there.
If you want to ignore the local settings which would make sense for currency you could do the following. (example in dollar with allways two decimal places)
decimal price = 12.10999999m;
String.Format("${0:0.00}", price);
$12.10
Be aware of that String.Format doesn't round the number correctly but just cuts it off.
I am looking to format a value in a datagridview.
These values are a string containing a decimal number. (Like "3000"
I want to display it with a thousand separator (space), like this: "3,000".
I know it can be done by assigning format of defaultcellstyle using format like "N2" for example, this works with decimal column type but
I'm using a string column type in my datagridview to handle some exception(displaying "-" instead of "0" to simplify users view)
I tried differents cell style format and nothing changed in the display.
do i need to change the column type of my datagridview or it can be done without too much code ?
Thanks for all reply,
Tristan
i just made it working as i expected. i did the commented things first and it just showed errors, so i tried the second way (simpliest) that is not commented.
string value = cell.Value.ToString();
//NumberFormatInfo nfi =(NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
//nfi.NumberGroupSeparator = " ";
//string formatted = double.Parse(value).ToString("n", nfi);
//cell.Value = formatted.Replace(".00","");// 12 345.00
string formatted = double.Parse(value).ToString("# ### ###");
cell.Value = formatted;
I am very new to coding, so this is likely a simple answer. I am trying to get my GUI button in C# to display the total of an arithmetic function I wrote. For example:
int totalGold = goldOnHand + earnedGold;
I have tried to display the totalGold as such in a text box name TxtGold:
private void BtnSplit_Click(object sender, EventArgs e) {
TxtGold.Text = "totalGold";
}
The text box only displays: totalGold
How do I get the textbox to display the integer that represents the total amount of gold instead of the phrase totalGold?
Thanks for any help from someone willing to give a hand to a noob!
In this code
int totalGold = goldOnHand + earnedGold;
You created a variable called totalGold. And you want to display it in a text box. That's so far so good. But when you try to set the text, things went wrong. You set the text of the text box to "totalGold".
In C#, "" means a string literal. Its value is "What you see is what you get". So when you say "totalGold", it displays the word totalGold. What you need to do is to remove the "" so that totalGold turns into a variable.
TxtGold.Text = totalGold;
But totalGold is an integer! you can only set the text of a text box using a string! How to convert from an integer to a string? Simple, use the ToString() method!
TxtGold.Text = totalGold.ToString();
Turn it into a string using the ToString() method:
TxtGold.Text = totalGold.ToString();
WHY:
What you were doing is setting the text of the button to a string literal, not the value of the variable.
Additionally, you cannot set TxtGold.Text to the integer, because it is a string property (see MSDN). Therefore, you have to do a ToString() to convert the integer to a string.
TxtGold.Text = "totalGold"; will print the string "totalGold" in your text box. If you need to print the integer value assigned to variable totalGold you have to print it as shown below
TxtGold.Text = totalGold.ToString();//that is, avoid the double quotes
the full code might be as follows
private void BtnSplit_Click(object sender, EventArgs e) {
int totalGold = goldOnHand + earnedGold;
TxtGold.Text = totalGold.ToString();
}
I have a basic understanding of C# and the .NET Framework, I have been given an assignment to build a POS (Point Of Sales) screen, I have currently hit a small brick wall trying to convert a currency related string back to a double.
I have two list boxes and several product buttons on the screen, the buttons are populated using a library class provided to us (essentially showing we can work with components)
One list box holds the product name while the other holds the price of that product, when a product button is selected it takes the product name from the buttons text and within its tag there is the price which is added to the list box of prices.
my problem is I want to show the prices in the List Box as a currency also that it shows all '0' I can do this no problem by doing either the following
value.ToString("C");
string.Format("{0:C}",value);
or using Convert etc.
Although because I have done this if I want to remove an item from the list by double clicking I need to take away the price from the total so I need to convert to back to double although because its in its current format I get an error when trying to perform that action I have looked around and I cannot seem to find anyway around it, the only option I can see is just leaving the string value as it is and not convert it to a currency format.
the ERROR: {"Input string was not in a correct format."}
Code Snippet
private void panelBtns_Click(object sender, EventArgs e)
{
Button panelBtn = (Button)sender;
lstProduct.Items.Add(panelBtn.Text);
double price = Convert.ToDouble(panelBtn.Tag);
>>CURRENCY FORMAT>> lstPrice.Items.Add(string.Format("{0:C}",price));
dblTotal = dblTotal + Convert.ToDouble(panelBtn.Tag);
lblTotal.Text = string.Format("{0:C}", dblTotal);
lblOutput.Text = "0";
lblOutput.Tag = "0";
}//End Panel Buttons
private void lstProduct_DoubleClick(object sender, EventArgs e)
{
int index = lstProduct.SelectedIndex;
lstPrice.SelectedIndex = lstProduct.SelectedIndex ;
>> ERROR HERE >> double price = Convert.ToDouble(lstPrice.GetItemText(lstPrice.SelectedItem));
dblTotal = dblTotal - price;
lstProduct.Items.RemoveAt(index);
lstPrice.Items.RemoveAt(index);
lblTotal.Text = string.Format("{0:C}", dblTotal);
}
Would anyone have any idea how I could possibly fix this, I had though about creating an invisible list to store the actual value of the tag so I can use that for later but would there be any other methods?
NOTE: I am also aware that using double for currency is not a very reliable
The easiest way to parse the C format is probably with
Double.Parse(text, System.Globalization.NumberStyles.Currency)
Of course you would always want to use Decimal to handle currency, and Decimal.Parse takes the same parameters.
In this case, though, you would want to store your internal numeric values along with their textual representation rather than converting to a string and then parsing back into a number.
other way, but please note, that you must try all cultures if removing symbol gives just decimal string, use Gabe's answer (posted just before mine :D)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Threading;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
//US-en culture / default I'm developing on
string currencyString = "$12.99"; //assuming got from: lstPrice.GetItemText(lstPrice.SelectedItem);
CultureInfo ci = Thread.CurrentThread.CurrentUICulture;
double d = Convert.ToDouble(currencyString.Replace(ci.NumberFormat.CurrencySymbol, ""));
//using custom culture
currencyString = "12.99zł";
ci = CultureInfo.GetCultureInfo("PL-pl");
d = Convert.ToDouble(currencyString.Replace(ci.NumberFormat.CurrencySymbol, ""));
}
}
}
You may want to look at assigning the decimal too with Math.Round Method
https://msdn.microsoft.com/en-us/library/system.math.round(v=vs.110).aspx