round of a big decimal value to small decimal value in c# - c#

I am working in c# and i do not want thing like this:
value = 1.658598554646 to value= 2
or
value = 1.2256485526 to value =1
I want to do something like :
value = 1.658598554646 to value = 1.7 //see upto one intger after point(.)
value = 1.2256485526 to value = 1.2
How to do that in c# ? Is there any way to achieve this ?

one decimal you can use Math.Round, and set the number of decimal points you round it to,
decimal value = 1.658598554646;
OneDec = Math.Round(value , 1);

How use Math.Round
var result = Math.Round(value, 1);

you can use
decimal DEBITAMT = Math.Round(1.658598554646, 1);

Related

Decimal rounding issues

I have a scenario in which some variable values have decimal values, the values can have leading .0, .234, .124, .125, numbers like this, and so on. If the number has leading decimal and zero it should ignore and if a number has leading 3 or more numbers it should round off to two.
Let's say the code is as follows:
var anone = "23"
var antwo = "23.0"
var anthree = "23.467"
var anfour = "23.125"
In order to remove the leading decimal and zero I have used the following method:
var removingzero = antwo.Replace(".0", "");
// The result will be = 23
In order to round off and limit the number to two decimal points I have used the following method:
var convertodecimal = Decimal.Parse(anthree);
var roundtotwo = Math.Round(convertodecimal, 2);
// The result will be = 23.47
similarly in order to convert the last one I follow the same method:
var convertodecimal = Decimal.Parse(anfour);
var roundtotwo = Math.Round(convertodecimal, 2);
// The result will be = 23.12
// But the Result should be = 23.13
So, the problem is when I am trying to round off any number like the last example it does not do it, how can I fix it.
It sounds like your question is a long way of asking "How can I force rounding UP when a decimal number ends in 5?"
If that's the case, then you can use the overload of Math.Round that takes a MidpointRounding argument, and specify either MidpointRounding.AwayFromZero or MidpointRounding.ToPositiveInfinity.
The behavior of these is the same for positive numbers, but difference is seen with negative numbers, where -23.125 will round to -23.13 if AwayFromZero is specified, or -23.12 if ToPositiveInfinity is specified.
So your code might look like this instead:
var roundtotwo = Math.Round(convertodecimal, 2, MidpointRounding.AwayFromZero);
Try by changing your Math.Round() to below one:
Math.Round(num,2, MidpointRounding.AwayFromZero)

How to get the next decimal given a decimal/precision

I want to determine the next decimal after a certain decimal programmatically.
For example, I have the decimal 0.0000041
I want to programmatically generate 0.0000042 in a function.
If I ran the function again after running it with 0.0000042 the new number would be 0.0000043
If I ran the function with the decimal being 0.0000049 the new number would be 0.0000050 etc....
Is there a way to do this?
I do not know where to start or what references to look at to do something like this.
Thank you.
You seem to be looking for something like this: (Check below if you want to specify the precision manually)
static decimal GetNextDecimal(decimal input)
{
int count = BitConverter.GetBytes(decimal.GetBits(input)[3])[2];
return input + (1m / (decimal)Math.Pow(10, count));
}
Usage:
decimal num1 = 0.0000041m;
decimal num2 = 0.00002m;
decimal next = GetNextDecimal(num1);
decimal next2 = GetNextDecimal(num2);
Console.WriteLine(next);
Console.WriteLine(next2);
Output:
0.0000042
0.00003
I used a little help of this answer to get the number of the decimal places.
If you originally wanted to specify the precision manually, you can use the following instead:
static decimal GetNextDecimal(decimal input, int precision)
{
return input + (1m / (decimal)Math.Pow(10, precision));
}
..which allows you to do something like this:
decimal num1 = 0.003m;
decimal next = GetNextDecimal(num1, 3);
decimal next2 = GetNextDecimal(num1, 4);
Console.WriteLine(next);
Console.WriteLine(next2);
Output:
0.004
0.0031

Split Double Value from textbox into multiple array

can someone show me how i can split value of double like this:-
654782.690
Can i split the value into this =
int i[0] = 65
int i[1] = 4
int i[2] = 7
int i[3] = 8
double i[4] = 2.690
Appreciate for the answer. Thank you in advance.
EDITED***
I have the UTM coordinate value in my xml. I want to make the chart and the value for the coordinate is fall into the grid. Lets say i have the zone area of x1 (578462.254), y1 (648523.124) & x2 (578481.452), y2 (648474.156). In my xml, i have a datapoint which fall under this coordinate. What i would like to do is to make the chart grid with the first two digit is become the zone area min and max(x,y).
I hope my clarification can explain what i want to do with those data. Thank you very much.
I got the answer here:-
decimal dValue = 654782.690;
string sValue = dValue.ToString();
decimal[] dArray = new decimal[5];
dArray[0] = Convert.ToDecimal(sValue.Substring(0, 2));
dArray[1] = Convert.ToDecimal(sValue.Substring(2, 1));
dArray[2] = Convert.ToDecimal(sValue.Substring(3, 1));
dArray[3] = Convert.ToDecimal(sValue.Substring(4, 1));
dArray[4] = Convert.ToDecimal(sValue.Substring(5));
Its worked now!!! Thank you for your support to solve my problem. :)
Use decimal instead of double if you are not storing scientific value.
decimal dValue = 654782.690M;
string sValue = dValue.ToString();
deciaml[] dArray = new decimal[5];
dArray[0] = Convert.ToDecimal(sValue.Substring(0,2));
dArray[1] = Convert.ToDecimal(sValue.Substring(2,1));
dArray[2] = Convert.ToDecimal(sValue.Substring(3,1));
dArray[3] = Convert.ToDecimal(sValue.Substring(4,1));
dArray[4] = Convert.ToDecimal(sValue.Substring(5));
To use the value of array index of 0 to 3 use Convert.ToInt32() to convert in Int datatype.
int iValue = Convert.ToInt32(dArray[0]);
Above example is not compiled it may give error. Take this example as a logic.

Round off in listview

I am trying to round off each row to the nearest 1.0 in a column of a listview, so meaning 1.58 should show 2.00 and 1.48 should be 1.00 -
Math.Round(listView1.Columns[2].ToString(), 10);
You need to use 0 in digits parameter. You expect no digits here, but you're passing 10 to digit parameter which says to round with 10 digits after decimal.
var res = Math.Round(1.58, 0);//2
var res = Math.Round(1.48, 0);//1
Just saw you try to Round the string, You'll have to convert it to Double or decimal or whatever.
var rounded = Math.Round(Double.Parse(listView1.Columns[2].ToString()), 0);
In case you want to get String as a final result (as far as you've put ToString() in your code), you may just use appropriate formatting string ("F0" in your case):
String result = (1.58).ToString("F0"); // <- "2"
...
String result = (1.48).ToString("F0"); // <- "1"
You can't round a string directly.
string val=listView1.Columns[2].ToString();
double i;
if(Double.TryParse(val, out i))
{
Console.WriteLine(Math.Round(i)); // you can use Math.Round without second
// argument if you need rounding to the
// nearest unit
}

C# format a double with ToString, and dispay zero value

I'm trying to find the best way to display a double in C# as follows:
7.345 should display as "73"
100.0 should display as "100"
0.234 shoud display as "02"
The input is a value between 0.00 and 10.00. I need to convert it to a filename. E.g. in case of a value of 5.4234, I should display "img54.jpg".
The problem is that I can't figure out how to display zero values in ToString() of doubles.
I tried this:
(10 * 7.345).ToString("##.") => correct
(10 * 10.00).ToString("##.") => correct
(10 * 0.000).ToString("##.") => FAIL, doesn't display anything.
(10 * 0.000).ToString("D2") => FAIL, D is not allowed in doubles
I can of course do some sophisticated string building, but if it's possible to use ToString, that would be much better of course.
Anyone an idea?
What do you want 0.00 to display as? "00"?
In that case you can try with format ToString("00.") instead.
Can you simply check if the double is 0? and If it is, set img00.jpg to your filename. That seems a lot easier than reworking your algorithm.
Why don't you convert it to an int?
int result = (int)(input * 10.00);
return result.ToString();
You could just convert to int before formatting like this:
((int)(10 * 7.345)).ToString("D2")
If you always have the input number of this format: #.###
You can multiply it by 1000 and divided by 100 and cast the result to an integer.
7.345 * 1000 = 7345 / 100 = 73.45 => Convert.ToInt32 => 73
0.000 * 1000 = 0 / 100 = 0 => Convert.ToInt32 = 0
Or you can multiply by 10 and convert to Integer.
return ((int)(input * 10.00)).ToString().SubString(0, 2);
double val = 7.345;
string result = val.ToString("0.#").Replace(".","");

Categories