Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a table that stores the amount of RAM a server has in a biginit column with values such as 2470208.
But how I can apply a data annotation or other validations to show only 2 instead of s470208. ?
I mean to always divide by 1 million and get the number on the left side of the digit ?
1) Use this for automatic thousands-unit:
string GetByteString(long n) {
int k=0;
string u=" kMGTEP";
while(n>1024) {
n>>=10;
k++;
}
return n.ToString() + u[k];
}
Call:
string s= GetByteString(1234567890123);
Debug.WriteLine(s);
2) But if you simply always want MB just shift by 20:
long n = 123456789;
string MB = (n>>20).ToString();
But this will show 0 if n goes below 1MB.
Reason:
1 kB = 2^10 = 1<<10 = 1024;
1 MB = 2^20 = 1<<20 = 1024*1024 = 1048576;
1 GB = 2^30 = 1<<30 = 1024*1024*1024 = 1073741824;
You tagged C# but mentioned a bigint column so it isn't clear whether you're looking for a database or C# solution. The following C# method will take the number of bytes as an integer and return a formatted string...
public string FormattedBytes(long bytes)
{
string units = " kMGT";
double logBase = Math.Log((double)bytes, 1024.0);
double floorBase = Math.Floor(logBase);
return String.Format("{0:N2}{1}b",
Math.Pow(1024.0, logBase - floorBase),
units.Substring((int)floorBase, 1));
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I need a method to return the firsts non zero numbers from a double in the following way: Any number >= 1 or == 0 will return the same; All the rest will return as per the following examples:
(Please note that I am using double because the potential imprecision is irrelevant in the use case whereas saving memory is relevant).
double NumberA = 123.2; // Returns 123.2
double NumberB = 1.2; // Returns 1.2
double NumberC = 0.000034; // Returns 3.4
double NumberD = 0.3; // Returns 3.0
double NumberE = -0.00000087; // Returns -8.7
One option would be to iteratively multiply by 10 until you get a number greater than 1:
public double RemoveLeadingZeros(double num)
{
if (num == 0) return 0;
while(Math.Abs(num) < 1) { num *= 10};
return num;
}
a more direct, but less intuitive, way using logarithms:
public double RemoveLeadingZeros(double num)
{
if (num == 0) return 0;
if (Math.Abs(num) < 1) {
double pow = Math.Floor(Math.Log10(num));
double scale = Math.Pow(10, -pow);
num = num * scale;
}
return num;
}
it's the same idea, but multiplying by a power of 10 rather then multiplying several times.
Note that double arithmetic is not always precise; you may end up with something like 3.40000000001 or 3.3999999999. If you want consistent decimal representation then you can use decimal instead, or string manipulation.
I would start with converting to a string. Something like this:
string doubleString = NumberC.ToString();
I don't know exactly what this will output, you will have to check. But if for example it is "0.000034" you can easily manipulate the string to meet your needs.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Currently I need to calculate 2^N, however N can be as large as 1929238932899 and I'm stuck using a long data type which can't hold a number that large.
I've currently tried converting to 'BigInt' however I'm still stuck with the long data type restriction from what I've seen as well.
I have a function which calculates the power. However, with the long data type, it just returns 0 when the number gets too big. Note that this is just a generic recursive power function.
For example, with 2^6 its meant to return 64 and with 2^47 to return 140737488355328. However, when it becomes 2^8489289, it just returns 0.
To represent 2^N in binary form, you need N+1 bits (binary digits), that is
(1 929 439 432 949 324 + 1) / 8 = 241 179 929 118 665.6 bytes ~ 219 PiB for a single number, if you really want to work with it.
Or you can just write down 2^N in binary form: 1 followed by N zeroes.
Since 2^N is an integer, you can represented it using Integer factorization.
You can put that in a class like this:
class FactorizedInteger {
private Dictionary<long, long> _factors = new Dictionary<long, long>();
public FactorizedInteger(long radix, long exponent) {
_factors[radix] = exponent;
}
public void Add(FactorizedInteger other) {
foreach(var factor in other._factors) {
if (_factors.ContainsKey(factor.Key)) {
_factors[factor.Key] += factor.Value;
} else {
_factors[factor.Key] = factor.Value;
}
}
}
public override string ToString() {
return "(" + String.Join(" + ", _factors.Select(p => $"{p.Key}^{p.Value}")) + ")";
}
}
As you can see, you can even add some mathematical operations without exhausting the memory of the computer. I've included Add as an example.
To use it:
var e1 = new FactorizedInteger(2, 1929238932899);
var e2 = new FactorizedInteger(2, 64);
Console.WriteLine(e1);
e1.Add(e2);
Console.WriteLine(e1);
Output:
(2^1929238932899)
(2^1929238932963)
This example needs to be made much smarter to be really usefull, but it is a possible representation of such large numbers.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How to get the whole number of this random.
Finally, I get a Debug.Log () to display my 757 number
You need to build up the whole number:
Random random = new Random();
int hundred = 100 * random.Next(1, 10);
int ten = 10 * random.Next(0, 10);
int one = random.Next(0, 10);
int result = hundred + ten + one;
Lets assume, the first random gives you 7, the second gives you 5 and the third gives you 7 again. you would end up with:
hundred = 100 * 7 = 700
ten = 10 * 5 = 50
one = 5
result = 700 + 50 + 7 = 757
Then just log result
What you describe is random 3 digit number. So simply:
Random.Range(100, 1000);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I find how many indivisible units of 1,000 are needed to overshadow a random number?
For example, for random number 5,123 I'm going to need 6 x 1,000 to overshadow it, so: MyAlgorithm(5123, 1000) = 6
Edit1: I am sorry if despite my endeavor to articulate my problem into a meaningful description my dyslexia took over, I hope this edit makes it a bit more comprehensible.
Well, if I understand your question, it sounds like you could simply convert the parameters to decimals, divide, then use Math.Ceiling:
int output = (int)Math.Ceiling((decimal)5123 / (decimal)1000); // 6
Alternatively, you could avoid the conversions and rely purely on integer division and the % operator (modulus), like this:
int output = (5123 / 1000) + (5123 % 1000 == 0 ? 0 : 1);
If you want this in a method simply wrap it up like this:
static int MyAlgorithm(int a, int b)
{
return (a / b) + (a % b == 0 ? 0 : 1);
}
if I've understood you correctly, this is really just a one-liner:
public static int MyAlgorithm(int input, int units)
{
return input%units == 0 ? input/units : input/units + 1;
}
the only case when it isn't simply the result of input/units + 1 is the case when there is no remainder
public int MyAlgorithm(int x, int y)
{
int result = x / y;
return (result < 0) ? (result - 1): (result + 1);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a quiz that have many questions and 5 possibilities of answers.
Lets take one question,it has follow answers:
148 - Good
5 - N/A
268 - Great
5 - Regular
11 - Bad
These are numbers collected directly from database.Now i need to show it as percentage.i.E:
Great - 45%
Good - 40
[..]
and so on
Any ideas?
int na = 5;
int good = 148;
int great = 268;
int regular = 5;
int bad = 11;
int sum = na + good + great + regular + bad;
naPercent = getPercent(na,sum);
float getPercent(int value, int sum)
{
return (value*100.0)/sum;
}
This is not a programming question, it is a math question. The percentage of each item is equal to the number of that item divided by the total number. In your example, the total number is 148+5+268+5+11 = 437. Great = 268 / 437 = 61.327%
total count for the answer / total count for all answers to this question combined * 100