|
|
|
|
![]() ![]() |
Mar 22 2006, 03:57 PM
Post
#1
|
|
|
Member [ Level 1 ] 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? |
|
|
|
Mar 22 2006, 04:52 PM
Post
#2
|
|
|
Nenad Bozidarevic Group: [MODERATOR] Posts: 1,049 Joined: 7-November 05 From: Belgrade, Serbia Member No.: 9,500 myCENTs:9.92 |
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 |
|
|
|
Mar 23 2006, 07:17 AM
Post
#3
|
|
|
Member [ Level 1 ] Group: Members Posts: 42 Joined: 17-March 06 From: Russia, St.Petersburg Member No.: 12,058 |
Thanks, You've helped a lot
|
|
|
|
Mar 24 2006, 05:44 AM
Post
#4
|
|
|
PsYcheDeLiC dR3aMeR Group: Admin Posts: 2,242 Joined: 29-January 05 From: Nakorn Chaisri, Thailand Member No.: 2,411 myCENTs:84.36 |
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. |
|
|
|
Mar 24 2006, 01:05 PM
Post
#5
|
|
|
Member [ Level 1 ] Group: Members Posts: 42 Joined: 17-March 06 From: Russia, St.Petersburg Member No.: 12,058 |
Thank you very much too, dear admin
|
|
|
|
Oct 24 2007, 09:34 AM
Post
#6
|
|
|
Newbie [ Level 1 ] 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?
|
|
|
|
Feb 23 2008, 06:51 PM
Post
#7
|
|
|
Newbie [ Level 1 ] 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 |
|
|
|
Apr 4 2008, 02:39 PM
Post
#8
|
|
|
Newbie [ Level 1 ] 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 |
|
|
|
Apr 7 2008, 05:12 AM
Post
#9
|
|
|
Premium Member Group: [HOSTED] Posts: 415 Joined: 16-February 06 From: Kolkata, India Member No.: 11,322 myCENTs:67.18 |
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(); } } |
|
|
|
Apr 7 2008, 05:52 PM
Post
#10
|
|
|
Newbie [ Level 2 ] Group: [HOSTED] Posts: 18 Joined: 22-March 08 Member No.: 29,297 |
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. |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 5th December 2008 - 05:26 PM |