Nov 21, 2009
Pages: 1, 2

Decrementing Number Of Digits Ofter Point - In C#

free web hosting

Read Latest Entries..: (Post #17) by iGuest on Aug 20 2009, 02:49 AM.
Replying to CrazyPensil just add .2f -reply by Sheen
Read the FIRST post of this Topic. - Express your Opinion! Contribute Knowledge :-).

Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > C# .NET

Decrementing Number Of Digits Ofter Point - In C#

CrazyPensil
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.3413543485
I wanna make it just 14.34.
My Informatic teacher says there is a way, but he'd forgot it. Could you, please, help me?

Comment/Reply (w/o sign-up)

pyost
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)/100

Don'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

Comment/Reply (w/o sign-up)

CrazyPensil
Thanks, You've helped a lot wink.gif

Comment/Reply (w/o sign-up)

miCRoSCoPiC^eaRthLinG
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:
CODE

// val contains your given value
int val = 14.3413543485;

// we declare a string that contains the truncated value
string 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.

Comment/Reply (w/o sign-up)

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

Comment/Reply (w/o sign-up)

FeedBacker
But if number is negative then val.ToString("N2"); returns 0. /Any solution for this?

Comment/Reply (w/o sign-up)

FeedBacker
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

Comment/Reply (w/o sign-up)

FeedBacker
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

Comment/Reply (w/o sign-up)

turbopowerdmaxsteel
QUOTE(FeedBacker @ Feb 24 2008, 12:21 AM) *
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.

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();

    }
}

 

 

 


Comment/Reply (w/o sign-up)

Doc.h0llyw00d
QUOTE(turbopowerdmaxsteel @ Apr 7 2008, 01:12 AM) *
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.

Comment/Reply (w/o sign-up)

Latest Entries

iGuest

Replying to CrazyPensil

just add .2f

-reply by Sheen

Comment/Reply (w/o sign-up)

iGuest
how to limit to numbers after the point in calculator program in C#
Decrementing Number Of Digits Ofter Point

Hi,

I am writing a financial calculator  program in C# and want to limit the No. Of digits after the Point button is clicked because it is only used for money, so only tow more digits can be typed. I used the indexof('.').ToString.Length == 2. But it did not work. 

Do you have any idea. 

-question by Omer

 


Comment/Reply (w/o sign-up)

iGuest
How to check the ODD or Even Number??
Decrementing Number Of Digits Ofter Point

Let me know how will I check the the given number is odd or Even numbers? Can you Help me! Give me a logic

-question by Ramesh

Comment/Reply (w/o sign-up)

iGuest
Ur suggestion helps me out
Decrementing Number Of Digits Ofter Point

Replying to miCRoSCoPiC^eaRthLinG

I read your reply and it helped me in solving my problem when I use N3 it is giving me the accurate values.I didn't test it for the -ve values

-reply by Ankit khanna

Comment/Reply (w/o sign-up)

iGuest
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

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Pages: 1, 2
Similar Topics

Keywords : Decrementing Digits Ofter


    Looking for decrementing, number, digits, ofter, point, c

See Also,

*SIMILAR VIDEOS*
Searching Video's for decrementing, number, digits, ofter, point, c
advertisement



Decrementing Number Of Digits Ofter Point - In C#

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com