C# number format: display n significant number of digits? - c#

is there a way to format a double value such that it shows the n siginifacant digits only?
for example I have a double with value 123456, can we have a format string such that it displays the first 3 digits only.
double x=12346;
string s=x.ToString("Some format"); //display 123 only
is that possible?

Although there are formats that would let you drop some or all of the fraction, no format would let you drop some of the significant digits of the whole part of the number.
If you would like to keep the first three digits of the whole number, you need to divide the value so that its whole part has only three digits.
One approach to computing the divisor is taking log10N, checking if it is above 2, and dividing by 10 to the corresponding power:
private static void Print(double x) {
int n = (int)Math.Log10(x);
if (n > 2) {
x /= Math.Pow(10, n-2);
}
Console.WriteLine((int)x);
}
Demo.

You can't format the largest part of the double to only be to 3 sig. fig. but you can split the string. Try:
String s = x.ToString().Substring(0, n);
Where n is the number of significant figures you wish to keep.

I made a console application for this example. I know you need a double or int data type for the number, but I didn't know how manipulate the decimal numbers after the point, so I used a string (if you don't mind, store the number value inside string data type):
string number = "";
string digits = "";
int n = 0;
int count = 0;
number = "45.6";
//number = "456";
n = 3;
if (number.Contains('.')) //If the number has decimals...
{
if (n < number.Length)
{
if (number.IndexOf('.') < n)
{
while (count <= n) //... we will count the number in a different way.
{
if (number[count] != '.')
{
digits = digits + number[count];
}
count++;
}
}
else
{
while (count < n)
{
if (number[count] != '.')
{
digits = digits + number[count];
}
count++;
}
}
}
}
else
{
if (n <= number.Length)
{
while (count < n) //If not, we count without the decimal point.
{
digits = digits + number[count];
count++;
}
}
}
Console.WriteLine("N significant digits: " + digits);
You can try with decimal and integer numbers, but in the code, they both are strings. As I said before, if you don't mind using this data type, this example will help you, if not, you can try with "Substring" function from the String class.

Related

Incorrect values when converting char digits to int

My end goal is to take a number like 29, pull it apart and then add the two integers that result. So, if the number is 29, for example, the answer would be 2 + 9 = 11.
When I'm debugging, I can see that those values are being held, but it appears that other values are also being incorrect in this case 50, 57. So, my answer is 107. I have no idea where these values are coming from and I don't know where to begin to fix it.
My code is:
class Program
{
static void Main(string[] args)
{
int a = 29;
int answer = addTwoDigits(a);
Console.ReadLine();
}
public static int addTwoDigits(int n)
{
string number = n.ToString();
char[] a = number.ToCharArray();
int total = 0;
for (int i = 0; i < a.Length; i++)
{
total = total + a[i];
}
return total;
}
}
As mentioned the issue with your code is that characters have a ASCII code value when you cast to int which doesn't match with the various numerical digits. Instead of messing with strings and characters just use good old math instead.
public static int AddDigits(int n)
{
int total = 0;
while(n>0)
{
total += n % 10;
n /= 10;
}
return total;
}
Modulo by 10 will result in the least significant digit and because integer division truncates n /= 10 will truncate the least significant digit and eventually become 0 when you run out of digits.
Your code is actually additioning the decimal value of the char.
Take a look at https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html
Decimal value of 2 and 9 are 50 and 57 respectively. You need to convert the char into a int before doing your addition.
int val = (int)Char.GetNumericValue(a[i]);
Try this:
public static int addTwoDigits(int n)
{
string number = n.ToString();
char[] a = number.ToCharArray();
int total = 0;
for (int i = 0; i < a.Length; i++)
{
total = total + (int)Char.GetNumericValue(a[i]);
}
return total;
}
Converted number to char always returns ASCII code.. So you can use GetNumericValue() method for getting value instead of ASCII code
Just for fun, I thought I'd see if I could do it in one line using LINQ and here it is:
public static int AddWithLinq(int n)
{
return n.ToString().Aggregate(0, (total, c) => total + int.Parse(c.ToString()));
}
I don't think it would be particularly "clean" code, but it may be educational at best!
You should you int.TryParse
int num;
if (int.TryParse(a[i].ToString(), out num))
{
total += num;
}
Your problem is that you're adding char values. Remember that the char is an integer value that represents a character in ASCII. When you are adding a[i] to total value, you're adding the int value that represents that char, the compiler automatic cast it.
The problem is in this code line:
total = total + a[i];
The code above is equal to this code line:
total += (int)a[i];
// If a[i] = '2', the character value of the ASCII table is 50.
// Then, (int)a[i] = 50.
To solve your problem, you must change that line by this:
total = (int)Char.GetNumericValue(a[i]);
// If a[i] = '2'.
// Then, (int)Char.GetNumericValue(int)a[i] = 2.
You can see this answer to see how to convert a numeric value
from char to int.
At this page you can see the ASCII table of values.
public static int addTwoDigits(int n)
{
string number = n.ToString()
char[] a = number.ToCharArray();
int total = 0;
for (int i = 0; i < a.Length; i++)
{
total += Convert.ToInt32(number[i].ToString());
}
return total;
}
You don't need to convert the number to a string to find the digits. #juharr already explained how you can calculate the digits and the total in a loop. The following is a recursive version :
int addDigit(int total,int n)
{
return (n<10) ? total + n
: addDigit(total += n % 10,n /= 10);
}
Which can be called with addDigit(0,234233433)and returns 27. If n is less than 10, we are counting the last digit. Otherwise extract the digit and add it to the total then divide by 10 and repeat.
One could get clever and use currying to get rid of the initial total :
int addDigits(int i)=>addDigit(0,i);
addDigits(234233433) also returns 27;
If the number is already a string, one could take advantage of the fact that a string can be treated as a Char array, and chars can be converted to ints implicitly :
var total = "234233433".Sum(c=>c-'0');
This can handle arbitrarily large strings, as long as the total doesn't exceed int.MaxValue, eg:
"99999999999999999999".Sum(x=>x-'0'); // 20 9s returns 180
Unless the number is already in string form though, this isn't efficient nor does it verify that the contents are an actual number.

Efficient way to count the number of appearances of a digit in a number

I need to count the number of times a single digit (not 0) appears in a number (positive integer) of varying length.
The obvious solution is to convert the number to a string, the digit to a character and iterate over the string to count the number of times the character appears in the string.
static int CountDigitInString(string searchString, char digit)
{
int sum = 0;
for (int i = 0; i < searchString.Length; i++)
{
if (searchString[i] == digit)
sum++;
}
return sum;
}
The problem with this method, however, is that it is too slow for my purposes as I am running it many times.
public static void Run()
{
for (int i = 0; i < 1000000; i++)
{
CountDigitInString(i.ToString(), (char)j);
}
}
After I noted that the process took too much time, the CPU sampling profiler showed me that the problem was with the conversion to string.
So, how do I efficiently count the number of times a digit (single digit only, not a number) appears in a number (of any length)?
Here is more optimized version of #shaitibber solution. It replaces one division with multiplying and returns 1 for 0,0. It is about 20% faster.
static int CountDigitsInString2(int number, int digit)
{
int sum = 0;
do
{
int n2 = number / 10;
if (number - n2 * 10 == digit)
sum++;
number = n2;
} while (number != 0);
return sum;
}
And here is solution about three times faster than that (but does not work for 0 digit, which is not required). It precalculates results for numbers 0..9999.
private static int[][] cache = new int[10][];
private const int cacheSize = 10000;//or 100000
private static int[] initCache(int digit)
{
var ca = cache[digit] = new int[cacheSize];
for (int i = 0; i < ca.Length; ++i)
{
ca[i] = CountDigitsInString2(i, digit);
}
return ca;
}
static int CountDigitsInString3(int number, int digit)
{
var ca = cache[digit] ?? initCache(digit);
int sum = 0;
while (number != 0)
{
int n2 = number / cacheSize;
sum += ca[number - n2 * cacheSize];
number = n2;
};
return sum;
}
I found a way which turned out to be about 3 times as fast on average (checked using a Stopwatch):
static int CountDigitsInString(int number, int digit)
{
int sum = 0;
while (number != 0)
{
if (number % 10 == digit)
sum++;
number /= 10;
}
return sum;
}
EDIT:
I found a way which is over 4 times as fast as the one above. Before I start, note that this solution is valid only for cases in which you are counting appearances of a digit in consecutive numbers.
It occurred to me that if you counted the number of times the digit "d" appeared in a number "A", then you don't neccessarily have to recount the number of times "d" appears in "A + 1" to know what it is.
For example, if I know that the digit 3 appears 4 times in the number 35312336, I can know for a fact that it will still appear 4 times in the next consecutive number 35312337, without actually counting.
The reason I can do this is that the count would only change in one of three cases:
1) When the last digit of "A - 1" was a 9, "A" can change entirely due to numbers being carried over. This is the only case in which we actually have to count (although you could, theoretically, optimize this further by checking the numbers carried over to see if they affect the total but this strikes me as overly complicated).
2) When the last digit of "A - 1" was "d - 1", we know that the number of times "d" appears in "A" has increased by one.
3) When the last digit of "A - 1" was "d", we know that the number of times "d" appears in "A" has decreased by one.
This means that you only have to count the appearances of "d" in "A" using arithmetical operations in one out of 10 cases!
public static void Run()
{
int digit = 1;
int count = 0;
for (int i = 0; i < 100000; i++)
{
int previousLastDigit = (i - 1) % 10;
if (previousLastDigit == (digit - 1))
count++;
else if (previousLastDigit == 9)
count = CountDigitsInString(i, digit);
else if (previousLastDigit == digit)
count--;
Console.WriteLine(digit + " appears " + count + " times in the number " + i);
}
}
The CountDigitsInString function is the one above.
Here is a little snip with LINQ to give you another way to do it (didn't ran a stopwatch)
var number = 11334511;
var digit = 1;
var digitAsChar = Convert.ToChar(digit.ToString().ToLower());
// Occurence will be 4
var occurence = number.ToString().ToLower().Count(s => s == digitAsChar);
I suggest you use LINQ on string.
Source: https://msdn.microsoft.com/en-us/library/mt693025.aspx
static int CountDigitInString(string number, char digit)
{
int count = number.Count(ns => ns == digit);
return count;
}

What number is a specified digit and how many of them are there in a number?

Ok, so at first, I'm very beginner in programming. It's my school homework and I cannot use conversion to string. Just if,else,for,while.
On input there is the number and the digit.
I know how to get information what number is a specified digit in a number but I have no idea how to find out how many of these numbers are there.
Let's say I have number 123 467 (it has to be less than 999 999) and I want the third number. I know it's bigger than 100 000, so I do the math - (int) 123 467 / 100 = 123 and then 123%10 = 3. Now I need to know if there are any more 3's in the number - but here is the point - I'm not sure what cycle should I use.
And I also have to create some code which determines how large is the number (bigger than 100/1000/10000/...).
I'm not asking for a full solution but little help would be appreciated. Even in a pseudolanguage.
Current code (almost nothing):
double digit, number;
try
{
digit = Convert.ToInt32(poledigit.Text);
number = Convert.ToInt32(polenumber.Text);
}
catch
{
MessageBox.Show("Zadejte číslo ve správném formátu");
return;
}
if (digit > 6 & number > 999999)
{
MessageBox.Show("Číslo musí být menší než 999 999 a digit musí být menší než 6.");
return;
}
while(number >= 100000)
{
number /= Math.Pow(10, digit);
number %= 10;
}
I would create an int array counting the number of digits
int[] digitCount = new int[10]; // Range: digitCount[0..9]
Then determine the digits one by one by eliminating the last one, until the number is zero. The loop would repeat the following code:
int digit = number % 10;
number /= 10;
digitCount[digit]++;
Now digitCount contains the count of each digit
int countOfDigit3 = digitCount[3];
If you cannot use arrays, count only the occurences of the desired digit
int digit = ...;
int digitCount = 0;
while (number != 0) {
int d = number % 10;
number /= 10;
if (d == digit) {
digitCount++;
}
}
You can iterate through the digits as follows:
int digitToSearch = 3;
int count = 0;
while (number != 0)
{
int digit = number % 10;
if (digit == digitToSearch)
count++;
number /= 10;
}

Double the alternative digits in a number and add each digit

I am using the following code to double alternative digits from a number and add every digit but its not working in c#
int sum=0,r,number;
Console.WriteLine("Enter the number");
number = int.Parse(Console.ReadLine());
if (number % 2!= 0)
{
number = number * 2;
number++;
}
Console.WriteLine("numbers:" + number);
Console.ReadLine();
while (number!= 0)
{
r = number % 10;
number= number/ 10;
sum = sum + r;
}
Console.WriteLine("sum of digits of the number:" + sum);
Console.ReadLine();
Kindly help me. Thank you in advance.
As StevieB said "You're not doubling alternative digits. You're doubling the number and adding 1 if the original number is odd. Then you're summing the digits of the number."
The following code should work for you. I have not tested it for all possible scenarios so please do so.
Also tweek it a little as per your requirements.
Console.WriteLine("Enter a number:");
char[] enteredNumber = Console.ReadLine().ToArray();
int finalNumber = 0;
for (int i = 0; i < enteredNumber.Count(); i++)
{
if (i % 2 != 0)//This condition might need tweeking
{
finalNumber = finalNumber + (Convert.ToInt32(enteredNumber[i].ToString()) * 2);
}
else
{
finalNumber = finalNumber + Convert.ToInt32(enteredNumber[i].ToString());
}
}
Console.WriteLine(finalNumber);
What you are looking for is Luhn Algorithm which I have reproduced here for you.
Luhn Algorithm - C#
Luhn Algorithm uses the last digit (rightmost digit) as a checksum digit. Hence in my first iteration I skip it since it's not required in the first parts of the function.
Since the length of a given number can vary, I have used int a to check my position and know which number I should double and which I should skip.
Read more about the Luhn Algorithm here.
using System;
public class Program {
public void Main()
{
int sum=0,d;
string oNum = "79927398713";
int a = 0;
for(int i = oNum.Length-2; i>=0;i--)
{
d = Convert.ToInt32(oNum.Substring(i,1));
if (a % 2 == 0) d = d*2;
if (d > 9) d -= 9;
sum += d;
a++;
}
if ((10 - (sum%10)) == Convert.ToInt32(oNum.Substring(oNum.Length-1)))
{
Console.WriteLine("Valid");
}
else
{
Console.WriteLine("Invalid");
}
Console.WriteLine("sum of digits of the number:" + sum);
}
}
There are three places where the Console writes a line. And at least two of those are visible for any given number. The line that you wanted, "sum of digits of the number:" will always show.

Get the seventh digit from an integer

I have a integer of 10 digits. I need to get the 7th digit of that integer.
I have found a mathematical solution to get the first digit of an integer.
var myDigit = 2345346792;
var firstDigit = Math.Abs(myDigit);
while (firstDigit >= 10)
{
firstDigit /= 10;
}
How can I get the seventh digit from myDigit? I am trying to avoid casting to string and doing a substring. I would like to see the mathemathical version of getting the seventh digit.
Anyone?
var seventh_digit = ( myDigit/1000000 ) % 10;
int getSeventhDigit(int number)
{
while(number >= 10000000)
number /= 10;
return number % 10;
}
This will take the last digit of numbers with 7 or less digits.
For numbers with 8 or more digits, it will divide by 10 until the number is 7 digits long, then take the last digit.
Mathematical solution without while loops:
int myDigit = 2345346792;
var seventh = (myDigit / 1000000) % 10;
//result should be 5, your seventh digit from the right
More generally, you can create a (zero-based) array from the digits:
uint myDigit = 2345346792;
int[] digits = new int[10];
for (int i = 9; i >= 0; i--)
{
digits[i] = (int)(myDigit % 10);
myDigit /= 10;
}
That should be useful for whatever manipulation you wish to do.
var nthDigit = (int)((number / Math.Pow(10, nth - 1)) % 10);
Where nth is n-th digit of the number.
Assuming that the "zeroth digit" is the least significant digit, this should do you:
public static int nthDigit( int value , int n )
{
if ( n < 0 ) throw new ArgumentException();
if ( value < 0 ) throw new ArgumentException() ;
while ( n-- > 0 )
{
value /= 10 ;
}
int digit = value % 10 ;
return digit ;
}
Something like this (C code, but should be readily portable):
if (n < 1000000)
return 0; // no 7th digit
while (n > 9999999)
n /= 10; // now in the range [1,000,000..9,999,999]
return n % 10;
This may seem outdated, but I wanted to get specific digits from a string of numbers and most of these solutions (and others I found) were lacking. So...I decided a workaround and converted the number to a string and then pulled the characters of the string, converting them back to an int. I know this isn't very efficient, but for small projects I don't concern myself with efficiency.
For my task, I was specifically working with phone numbers, so here is my code:
long number = 5559876543;
string s = "" + number;
int areaCode = int.Parse(s.Substring(0, 3));
int prefix = int.Parse(s.Substring(3, 3));
int suffix = int.Parse(s.Substring(6, 4));
I hope this helps anyone looking for a similar solution.
public static int GetNthDigit(this int value, int digits)
{
double mult = Math.Pow(10.0, digits);
if (value >= mult)
{
while(value >= mult)
value /= 10;
return value % 10;
}
else
{
throw new ArgumentOutOfRangeException("Digits greater than value");
}
}

Categories

Resources