Welcome Guest ( Log In | Register )



2 Pages V   1 2 >  
Reply to this topicStart new 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,002
Joined: 7-November 05
From: Belgrade, Serbia
Member No.: 9,500



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,242
Joined: 29-January 05
From: Nakorn Chaisri, Thailand
Member No.: 2,411



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: 374
Joined: 16-February 06
From: Kolkata, India
Member No.: 11,322



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

    }
}
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) *
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 topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Cracking Wireless Access Point Password?(22)
  2. Hinduism - The Breeder Of World Religions?(23)
  3. How Can I Create A "number Of Visitors" Script(8)
  4. Mysql Question(inserting Number From A Textfield)(3)
  5. Finding The Current Line Number In A Text Box(1)
  6. Get The Most From Your Post(20)
  7. What Is The Best Way To Merge An Unknown Number Of Arrays?(4)
  8. Maximum Number Of Decimal Places In Perl Language(0)
  9. Necklace Problem(8)
  10. How To Limit The Number Of Pages In Ms Word?(6)
  11. Hints Point That Dell Will Be Pre-installing Ubuntu Soon!(22)
  12. Auto-number Help In Access Db & Vb .net(6)
  13. Visitor Hit Counter Script?(7)
  14. Reliable Hardware Serial Number For Software Protection?(10)
  15. What Is: World's Largest Known Prime Number(20)
  1. The Sum Of The Cubes Of The Digits Of Any Non-negative Integer(5)
  2. My Xbox 360 Vs. Ps3 Vs. Wii Review(3)
  3. Yeppee ! Post Number One Thousand !(5)
  4. Help Me To Edit Mx Records To Point To Google Apps(5)
  5. Count The Number Of Records Of A Table Group By A Foreign Key(0)
  6. Domain Name Parking(8)
  7. Fibonacci Number Program Problem(5)
  8. Prime Number Generator(1)
  9. Interested In Acquiring Webspace(2)
  10. Can I Please Have The 30 Point Hosting Pack ?(1)
  11. WAP54G Linksys Wireless Access Point(7)
  12. A Small Project: Splitting An Integer Into Digits(5)
  13. Neopets News(14)
  14. Need Drivers For Intraserver SCSI Adapter(2)
  15. At One Point, I Thought All Free Web Hosts Sucked(4)


 



- Lo-Fi Version Time is now: 8th September 2008 - 07:09 AM