Hi,
I have avalue like 2.90
i want the value to be round off to 3.00
Can u help me out
Rajiv
Hi,
I have a value like 2.90
i want the value to be round off to 3.00
can u provide me coding
Can u help me out
Rajiv
You can use
Math.Round(<%value%>, <%roundoff decimaltoints%>)
for eg,
Math.Round(6.42, 1)
Hope this helps u... :-D
Rounding off from 2.90 to 3.00 is very unusual. 2.90 is rounded to 3, for that you can use asl ss125 says Math.Round. You cannot use Math.Round(2.90, 2) to return 3.00, it will return 2.90!
no it want help it still shows 6.4 instaed of 6.00
fine...as far as i know generally we use the basic roundoff function. But considering your question,I want to answer my question.
What type of rounding do you want?
6.4 => 6
or
6.4 => 7
or
if 6.4=>6.4 but 6.9=>7
FOR WHICH ONE DO YOU NEED THE LOGIC?
I require both logic
Fine. I dont know whether VB.NET has any options to do with it.. But If you need a custom round function,I will tell you the logic to create your own round function.
1) Get the input in ther string format.
2) Using substring(),find the location of the [.] in the value.
3) now remove all the other number after the first digit of the decimal point using trim().
eg: 6.758698955 => 6.7
4) Store the value before [.] in a variable.
consider it as variable j
5) then get the value after the decimal point.
6) convert it into integer.
7) now in if condition you have to check two conditions.
7.1) if(i<5)
7.2) if(i>=5 and i!=9)
7.3) if(i=9)
8) Write appropriate code for each statements such as
if(1<5)
do nothing
ie., 6.4 => 6.4
if(i>=5 and i!=9)
do calculations based on what type do you want.if u don't need to do anything, skip this segment.
here is the important thing
if(i=9)
i=o
j=j+1
textbox1.text=i+"."+j
Hope this logic helps you my friend. :-D
You could try
Math.Truncate(yournum + 0.5)
and convert it to Int if you require an integer. I hate the way vb does rounding which is different for even numbers than for odd.
Math.Round(6.5) is 6.0
Math.Round(5.5) is 6.0
Try this
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
decimal pi = new decimal(3.14159265);
decimal piRoundedUp = Math.Round(pi, 4);
decimal piTruncated = Math.Round(pi, 4, MidpointRounding.AwayFromZero); //3.1416
// MidpointRounding.AwayFromZero is use
// to Round 2.5 to 3 where
// MidpointRounding.ToEven rounds to 2
// See http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx
Console.WriteLine("{0:0.0000}", pi); //normal round
Console.WriteLine("{0:0.0000}", piTruncated); //as above
// first multiply with 10^(number of decimals)
// and then devide with 10^(number of decimals)
// because Floor gets the integer part of the decimal.
decimal piFloored = Math.Floor(pi * (decimal)Math.Pow(10, 4)) / (decimal)Math.Pow(10, 4);
Console.WriteLine("{0:0.0000}", piFloored); //3.1415
Console.ReadKey();
}
}
}
i will give u one example below :
amt = format(1000+2.6+3.5, "#,##0.00")
the above give me the result is 1,006.10 which iclude coma and two decimal place except round off
but if i will give this following formula :
amt = Round(1000+2.6+3.5, "0.00")
then the above give me the result is 1006 which is include Round off but not include coma and two decimal place
i want that formula which give Coma, two place decimal and Round off
i will give u one example below :
amt = format(1000+2.6+3.5, "#,##0.00")
the above give me the result is 1,006.10 which iclude coma and two decimal place except round off
but if i will give this following formula :
amt = Round(1000+2.6+3.5, "0.00")
then the above give me the result is 1006 which is include Round off but not include coma and two decimal place
i want that formula which give Coma, two place decimal and Round off like 1,006.00
Would something like this work:
FormatNumber(CInt(1000 + 2.6 + 3.5)), 2)
Just let the framework do the formatting for you.
THANKS IT SOLVES
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.