Jump to content
xisto Community
Sign in to follow this  
CrazyPensil

Decrementing Number Of Digits Ofter Point In C#

Recommended Posts

Hi! I've been solving a problem on C# and a one problem had gone off. I need to decrease number of digits of a float variable.for example:If I have 14.3413543485I wanna make it just 14.34.My Informatic teacher says there is a way, but he'd forgot it. Could you, please, help me?

Share this post


Link to post
Share on other sites

I am not sure of the commands, because I've only just started programming in C#, but I can give you something you could use.Let's say your number is x=12.5742165 and you want it to be x=12.57. Here's a formula to use:x=trunc(x*100)/100Don't know if trunc is the right command, but you need one that removes the part after the '.'This way, you get x=1257/100 and that is 12.57

Share this post


Link to post
Share on other sites

Thanks, You've helped a lot :o

Share this post


Link to post
Share on other sites

There's a very easy way to do this in C# - by passing a certain parameter to the ToString method that is part of any given data type or canonical class.

 

For example:

// val contains your given valueint val = 14.3413543485;// we declare a string that contains the truncated valuestring decimal_adjusted = val.ToString ( "N2" );

The resultant value in decimal_adjusted should be 14.34.

 

The N2 value passed to the ToString method tells it to treat the value stored in val as a Numeric value and cut it down to 2 decimal places.

 

You can replace 2 with whatever your desired degree of truncation is. If you want upto 3 decimal places, it should be N3, for 5 decimal places, N5 and so on.

Share this post


Link to post
Share on other sites

Thank you very much too, dear admin :lol: I havent't thought about this one :lol: a good method. anyway, the problem is already solved;)

Share this post


Link to post
Share on other sites

write a C# program to find out the sum of odd & even digits in a given large integer. for Ex:- input= 147936 and output:- odd total-1+7+3=11 & even total-4+9+6=19.

Decrementing Number Of Digits Ofter Point

 

I need a complete C# program of

Write a C# program to find out the sum of odd & even digits in a given large integer. For Ex:- input= 147936 and output:- odd total-1+7+3=11 & even total-4+9+6=19.

 

Can any one of you give me this program?

 

-reply by malleswararao

Share this post


Link to post
Share on other sites

How To Remove other digits after decimal points .

Decrementing Number Of Digits Ofter Point

 

Now I am using MsSql With C#.

 

I Want To take Decimal value From Table and display in text Field.

 

But My problem is that The Values in Tables have 4 digits after decimal point but I want to show only two Point.

 

So Please Help me.

 

-question by Sandip Dhage

Share this post


Link to post
Share on other sites

write a C# program to find out the sum of odd & even digits in a given large integer. for Ex:- input= 147936 and output:- odd total-1+7+3=11 & even total-4+9+6=19.

 

Decrementing Number Of Digits Ofter Point

I need a complete C# program of

Write a C# program to find out the sum of odd & even digits in a given large integer. For Ex:- input= 147936 and output:- odd total-1+7+3=11 & even total-4+9+6=19.

 

Can any one of you give me this program?

 

-reply by malleswararao


Thats easy enough. The trick is in converting the number to string and then taking out one character at a time using the string type's substring method in a loop. Use the counter variable to check if the current position is odd or even and correspondingly add the digit to the running sum of Even or Odd numbers. Given below is the code.

 

using System;class Program{	static void Main(string[] args)	{		Console.Write("Enter the number:");		int Num = Convert.ToInt32(Console.ReadLine());		string NumInString = Num.ToString();		int OddSum = 0, EvenSum = 0;		for (int i = 0; i < NumInString.Length; i++)		{			int Digit = Convert.ToInt32(NumInString.Substring(i, 1));			if (i % 2 != 0)				EvenSum += Digit;			else				OddSum += Digit;		}		Console.WriteLine("Odd Sum = {0} Even Sum = {1}", OddSum, EvenSum);		Console.Read();	}}

Share this post


Link to post
Share on other sites

Thats easy enough. The trick is in converting the number to string and then taking out one character at a time using the string type's substring method in a loop. Use the counter variable to check if the current position is odd or even and correspondingly add the digit to the running sum of Even or Odd numbers. Given below is the code.


Your method seems excessive, why not just retrieve the value and then truncate it. While it probably doesn't matter for the scope of his problem, using loops while going through massive data can be taxing on resources. Granted the first explanation wasn't very good, but if you look up the truncate function in the Microsoft C# reference library it will explain how to use it, I'm sure it will fit your needs.

@Sandip is there a reason you can't truncate the value? If so please post it and I'll give you some more feedback.

Share this post


Link to post
Share on other sites

Your method seems excessive, why not just retrieve the value and then truncate it. While it probably doesn't matter for the scope of his problem, using loops while going through massive data can be taxing on resources. Granted the first explanation wasn't very good, but if you look up the truncate function in the Microsoft C# reference library it will explain how to use it, I'm sure it will fit your needs.
@Sandip is there a reason you can't truncate the value? If so please post it and I'll give you some more feedback.


I did not get what you mean to say by using the Truncate method. The Decimal.Truncate() method returns the nearest whole number. While the given problem has got nothing to do with points. Am I missing out on something or are you confusing the two feedbacks? I have answered to malleswararao's problem of determining the sum of digits at even and odd positions in a given number.

Share this post


Link to post
Share on other sites

are you confusing the two feedbacks?

This is absolutely the case. Sorry, I was answering the question directly above your post, figured you were doing the same as well seeing as how the post you responded to was from February 23rd, 2008.

The guy who wanted to make 4 decimal points into two (Sandip, was his name-o)
you need to multiply the number out so that those two decimal points are part of the whole number,
then truncate the value, then divide by the amount you multiplied by, and you have your two digits instead of 4.

Share this post


Link to post
Share on other sites

what is " N2 " here give some details of it to use in proper manner.

Decrementing Number Of Digits Ofter Point

 

What is " N2 " here give some details of it to use in proper manner.

 

-reply by yashwant

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.