Reputation: 61
private void textBox1_TextChanged(object sender, EventArgs e)
{
decimal x = 0;
if (Decimal.TryParse(textBox1.Text, out x))
{
var y = 1000000;
var answer = x * y;
displayLabel2.Text = x.ToString();
}
else
{
displayLabel2.Text = "error";
}
}
In my code if the user types in 7.2 it will display 7.2. And if you type in 0.72 it will display it as so. And so on. I am trying to only take the 2 digits that aren't 0 though. I need it inputted as a decimal for a conversion. But I then need to take the 2 main numbers and display it. so if I type in 0.00072, I only need to display 72.
The catch is, if I type in 0.001 or 0.0010, I need to display 10. I have no idea where to start with this. I tried using length to find the numbers, but because I am using decimals, I cannot use "Length".
Upvotes: 0
Views: 42
Reputation: 1718
displayLabel2.Text=(x.ToString().Replace(".","").TrimStart(new Char[] {'0'})+"00").Substring(0,2) ;
Upvotes: 0
Reputation: 53958
That you want is to trim the leading zeros. This can be done like below:
var trimmedValue = x.ToString(CutlureInfo.InvariantCulture)
.TrimStart(new Char[] {'0'});
if(trimmedValue.Length == 1) trimmedValue = trimmedValue+"0";
displayLabel2.Text = trimmedValue;
Upvotes: 2