C# convert round up textbox - c#

I have a text box where I can enter numeric data which I can have decimals up to two decimal places, example 125.02 or just 125. However, I want to round all data that is entered with a decimal up, example 125.55 would equal 126. Below is a snippet of my broken code. Any suggestions would be great!
int num6 = (int)Math.Ceiling(textBox5.Text);

I think you need to covert the string to a double. Try this:
int num6 = (int)Math.Ceiling(double.Parse(textBox5.Text));
or safer to use double.TryParse:
int num6;
double d;
if(double.TryParse(textBox5.Text, out d)
{
num6 = (int)Math.Ceiling(d);
} else {
//Bad input value - perhaps flag this to user
}

Math.Ceiling method has 2 overloads;
Math.Ceiling Method (Double)
Math.Ceiling Method (Decimal)
It doesn't have an overload takes string as a parameter. You need to parse your string like;
decimal d;
if(Decimal.TryParse(textBox5.Text, out d))
{
int num6 = (int)Math.Ceiling(d);
}

Related

How to add precision to decimal value

I want to add precision to the decimal value. For example, I have this value:
decimal number = 10;
I want to make it 10.00. I don't want to convert it to string like number.ToString("#.00")
Currently, I have this method:
decimal CalculatePrecision(decimal value, int precision)
{
var storedCalculated = decimal.Divide(1, Convert.ToDecimal(Math.Pow(10, precision)));
return value + storedCalculated - storedCalculated;
}
Is there any good solution for this?
You can't. 10 and 10.00 are the same number. Only the "presentation" is different. Both "presentations" are strings. The actual number look different. If you need to change the presentation, convert to string.
How about
decimal d = 10;
d += 0.00M;
Console.WriteLine(d);
Try reference
Math.Round not keeping the trailing zero
How do I display a decimal value to 2 decimal places?

Convert decimal with comma and floating point

i want to parse like:
3.5 -> 3.5
3.484 -> 3.48
3.82822 -> 3.82
etc.
However,
decimal.Parse("3.543")
yields 3543 and
so i did:
decimal.Parse("3.543",CultureInfo.InvariantCulture)
yields 3.543 and
but
decimal.Parse(String.Format("{0:0.00}","3.543"),CultureInfo.InvariantCulture);
yields 3543
so how can i do it???
You need Round method:
decimal t = 3.82822;
decimal.Round(t, 2);
Where 2 show the decimal points you need.
Use Math.Round like this:
decimal a = 1.9946456M;
Math.Round(a, 2); //returns 1.99
decimal b = 1.9953454M;
Math.Round(b, 2); //returns 2.00
I guess you want to truncate the decimal places after two digits. Give this a try:
public decimal TruncateDecimal(decimal value, int precision)
{
decimal step = (decimal)Math.Pow(10, precision);
int tmp = (int)Math.Truncate(step * value);
return tmp / step;
}
decimal t = 3.82822;
decimal d = TruncateDecimal(t, 2);
You should use a culture that actually uses a comma as a decimal seperator.
CultureInfo.GetCultureInfo("fr-fr")
for example.
Console.WriteLine(decimal.Parse("3,543", CultureInfo.InvariantCulture)); // 3543
Console.WriteLine(decimal.Parse("3,543", CultureInfo.GetCultureInfo("fr-fr"))); //3,543
and if you want to round the result. You could use
Console.WriteLine(String.Format("{0:0.00}", decimal.Parse("3,543", CultureInfo.GetCultureInfo("fr-fr")))); //3,54

Calculating with decimal

I'm trying to get the comma number of my int after having calculated it, but i can't seem to get it to work.
My code:
int price = 120;
decimal calc = price / 100;
But it only returns 1.
int price = 120;
decimal calc = price / 100m;
your variant:
int price = 120;
int temp = price / 100;// temp = 1
decimal calc = (decimal) temp;
int price = 120;
decimal calc = ((decimal)price) / 100;
You canculation is being done in integer type as both the operands are integer. It should be:
decimal calc = price / 100M;
// ^^^^^
//atleast one of the operand should be decimal
Or
decimal calc = (decimal)price / 100;
When you divide an integer by another integer, result is always an integer. Since you want your answer in more precise way you need to typecast it based on precision that you want. Decimal gives you best possible precision in C#. But even casting to float or double would have also given you answer in the format that you expected. Casting again depends on level of accuracy needed. Here is more detailed explanation from MSDN.
The simplest way is declare price as decimal also
decimal price=120;
decimal calc=price/100;
If it is from an argument or another local variable, you can still store it in decimal like:
int priceInInt=120;
decimal price=priceInInt;
decimal calc=price/100;

Float / Float = strange result

I have two values, one from user input and another from DB.
var userinput = form["someInput"];
var valuefromDB = GetValue(someNumber);
public float? GetValue(int id){
return (float?) db.table.where(p=> p.id == id).select(p=> p.Value).SingleOrDefault();
}
userinput have value "1" as string, while valuefromDB havevalue 0.001 as float.
so 1 / 0.001 = 1000
but my c# code give me 999.999939 as result;
var final = float.Parse(userinput) / valuefromDB
when i have "2" as user input value, result is correct, 2000...
That's because not all decimal numbers can be accurately represented in binary (which is the representation that float uses). The solution is to format the result to the desired number of decimal places, which will cause it to be rounded and displayed "correctly" as a consequence.
Update: To format a float for display, take a look at this MSDN reference page and this page of examples.
For pure precision which is not provided by float use decimal instead.
See What is the difference between Decimal, Float and Double in C#?

How to round a decimal for output?

Using C#, I want to format a decimal to only display two decimal places and then I will take that decimal and subtract it to another decimal. I would like to be able to do this without having to turn it into a string first to format and then convert it back to a decimal. I'm sorry I forget to specify this but I don't want to round, I just want to chop off the last decimal point. Is there a way to do this?
If you don't want to round the decimal, you can use Decimal.Truncate. Unfortunately, it can only truncate ALL of the decimals. To solve this, you could multiply by 100, truncate and divide by 100, like this:
decimal d = ...;
d = Decimal.Truncate(d * 100) / 100;
And you could create an extension method if you are doing it enough times
public static class DecimalExtensions
{
public static decimal TruncateDecimal(this decimal #this, int places)
{
int multipler = (int)Math.Pow(10, places);
return Decimal.Truncate(#this * multipler) / multipler;
}
}
You can use: Math.Round(number,2); to round a number to two decimal places.
See this specific overload of Math.Round for examples.
Math.Round Method (Decimal, Int32)
You don't want to format it then, but to round it. Try the Math.Round function.
Take a look at Math.Round

Categories