Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
I am creating windows application using c# 2010, in my application I got a values on text box in decimal points, how to set my decimal point <0.5 set round off or  > 0.5 add +1 in my decimal values.

Ex :
70.04 = here I want 70
70.05 here I want 71

Any one give me any ideas.


What I have tried:

C#
How to set in decimal point round off
Posted
Updated 7-Dec-16 22:14pm
Comments
lukeer 8-Dec-16 2:32am    
The second example would be plain wrong. Don't you mean
70.05 -> 70.1 or
70.5 -> 71 instead?
Boopalslm 8-Dec-16 2:47am    
in my value <.13 here i want set to .1, other wise >.18 is set to .2
Peter Leow 8-Dec-16 4:02am    
Be clear. You said "<0.5 set round off or > 0.5 add +1 in my decimal values", what happens to "0.5"?

Be clear of what you want. Be clear. You said "<0.5 set round off or > 0.5 add +1 in my decimal values", what happens to "0.5"? Let's follow this Rounding Numbers[^]
In c#, you can use Math.Round with MidpointRounding.AwayFromZero option[^] to achieve your desired rounding result.
But before that, you have to check the validity of the decimal number from the text box by using Decimal.TryParse Method (String, Decimal) (System)[^]. Putting things together, it looks like this:
C#
using System;

public class Program
{
	public static void Main()
	{
		string value;
		decimal number;
		value = "70.13";
		if (Decimal.TryParse(value, out number))
	
   			Console.WriteLine(Math.Round(number,1,MidpointRounding.AwayFromZero));
		else
   			Console.WriteLine("Unable to parse '{0}'.", value);
	}
}
 
Share this answer
 
v3
C#
static void Main(string[] args)
      {
          decimal val1 = 10.51M;
          decimal val2 = 10.49M;

           val1 = Math.Round(val1,1);
           val2 = Math.Round(val2,1);
           Console.WriteLine(val1);
           Console.WriteLine(val2);
          Console.ReadLine();
      }
 
Share this answer
 
Comments
Boopalslm 8-Dec-16 3:04am    
here i want for example i got a value in my text box 33.16 - i want 33.20 other wise less than ex 33.14 i want 33.10 - how to crate.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900