Welcome Guest ( Log In | Register )




                Web Hosting Guide

2 Pages V   1 2 >  
Reply to this topicNew Topic
Decrementing Number Of Digits Ofter Point, In C#
CrazyPensil
post Mar 22 2006, 03:57 PM
Post #1


Member [ Level 1 ]
Group Icon

Group: Members
Posts: 42
Joined: 17-March 06
From: Russia, St.Petersburg
Member No.: 12,058


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?
Go to the top of the page
 
+Quote Post
pyost
post Mar 22 2006, 04:52 PM
Post #2


Nenad Bozidarevic
Group Icon

Group: [MODERATOR]
Posts: 1,087
Joined: 7-November 05
From: Belgrade, Serbia
Member No.: 9,500
myCENTs:42.34


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
Go to the top of the page
 
+Quote Post
CrazyPensil
post Mar 23 2006, 07:17 AM
Post #3


Member [ Level 1 ]
Group Icon

Group: Members
Posts: 42
Joined: 17-March 06
From: Russia, St.Petersburg
Member No.: 12,058


Thanks, You've helped a lot wink.gif
Go to the top of the page
 
+Quote Post
miCRoSCoPiC^eaRt...
post Mar 24 2006, 05:44 AM
Post #4


PsYcheDeLiC dR3aMeR
Group Icon

Group: Admin
Posts: 2,248
Joined: 29-January 05
From: Bangkok, Thailand
Member No.: 2,411
myCENTs:19.10


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.
Go to the top of the page
 
+Quote Post
CrazyPensil
post Mar 24 2006, 01:05 PM
Post #5


Member [ Level 1 ]
Group Icon

Group: Members
Posts: 42
Joined: 17-March 06
From: Russia, St.Petersburg
Member No.: 12,058


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;)
Go to the top of the page
 
+Quote Post
iGuest
post Oct 24 2007, 09:34 AM
Post #6


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869


But if number is negative then val.ToString("N2"); returns 0. /Any solution for this?
Go to the top of the page
 
+Quote Post
iGuest
post Feb 23 2008, 06:51 PM
Post #7


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869


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
Go to the top of the page
 
+Quote Post
iGuest
post Apr 4 2008, 02:39 PM
Post #8


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869


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
Go to the top of the page
 
+Quote Post
turbopowerdmaxst...
post Apr 7 2008, 05:12 AM
Post #9


Premium Member
Group Icon

Group: [HOSTED]
Posts: 447
Joined: 16-February 06
From: Kolkata, India
Member No.: 11,322
myCENTs:81.67


QUOTE(FeedBacker @ Feb 24 2008, 12:21 AM) [snapback]119434[/snapback]
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();

    }
}
Go to the top of the page
 
+Quote Post
Doc.h0llyw00d
post Apr 7 2008, 05:52 PM
Post #10


Newbie [ Level 2 ]
Group Icon

Group: [HOSTED]
Posts: 18
Joined: 22-March 08
Member No.: 29,297


QUOTE(turbopowerdmaxsteel @ Apr 7 2008, 01:12 AM) [snapback]121502[/snapback]
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.
Go to the top of the page
 
+Quote Post

2 Pages V   1 2 >
Reply to this topicNew Topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   11 tansqrx 4,697 16th December 2009 - 12:40 AM
Last post by: iG-David Schwartz
No New Posts   6 wutske 3,207 3rd December 2009 - 01:44 PM
Last post by: iG-gigi
No New Posts   10 yordan 2,539 27th October 2009 - 11:31 PM
Last post by: HannahI
No New Posts   3 lalhsboard 4,402 30th June 2009 - 04:51 PM
Last post by: iG-Icecycle
No new   22 essential_clix 14,787 7th August 2008 - 09:27 PM
Last post by: Guest
No New Posts   10 ganeshn11 2,262 21st April 2008 - 11:15 AM
Last post by: Jared
No New Posts   3 Feelay 724 1st March 2008 - 03:51 AM
Last post by: TavoxPeru
No New Posts   1 CaptainRon 5,448 18th January 2008 - 06:02 AM
Last post by: iGuest
No New Posts   4 vujsa 941 9th November 2007 - 11:40 PM
Last post by: vizskywalker
No New Posts   0 al-rafideen 2,116 19th October 2007 - 09:53 AM
Last post by: al-rafideen
No New Posts 6 miCRoSCoPiC^eaRthLinG 3,590 7th July 2007 - 01:02 AM
Last post by: Jimmy89
No new   22 nightfox 2,463 13th May 2007 - 05:52 PM
Last post by: nightfox
No New Posts   6 dhanesh 1,376 2nd May 2007 - 12:23 PM
Last post by: faulty.lee
No new 20 miCRoSCoPiC^eaRthLinG 5,806 28th December 2006 - 11:22 PM
Last post by: borlafu
No New Posts   5 demolaynyc 3,871 8th December 2006 - 11:05 AM
Last post by: pyost


Web Hosting Powered by ComputingHost.com.
HONESTY ROCKS! truth rules.
Creative Commons License