How can I reduce the number of figures of a number? [closed] - c#

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
Is there a way to reduce the number of figures of a number?
Example:
double d = 222222222222222224444444444444.0
I want to "serialize" it like 17[2]13[4] for example.
The idea is to reduce the number of "chars" used by the number.

double d = 222222222222222224444444444444.0
You can't have a double that big in the first place.
I want to "serialize" it like 17[2]13[4] for example.
The idea is to reduce the number of "chars" used by the number.
A double only takes 8 bytes regardless of its value. There doesn't seem to be any actual point to this.

Related

Creating 100 variables [closed]

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 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
So I created an array with 100 variables using Enumerable.Range. The data type is limited to Int32.
Problem
How can I create the same array with SByte?
Am I right in thinking I would need to use a loop to create and index the variables?
I have looked around online and most results touch on declaring counting variables for the loop but not using a loop to declare variables
Just cast them:
SByte[] array = Enumerable.Range(0, 100).Select(i => (SByte) i).ToArray();
note that SByte is not cls compliant, you might want to use short instead.

Convert byte array to integer with lower and upper bound [closed]

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
I have a method that pseudo-randomly encrypts a byte array. I would like to convert the encrypted byte-array to a C# int (4 bytes), while observing a user-specified lower and upper bound (e.g. give me a number between 1 and 10)
What is the most secure and performant way of achieving this?
You could use System.ByteConverter.ToInt32(arr, start_index).
From your description of the array, the start index would likely be zero.
I am not sure what you mean by upper and lower bound, but if you mean you want a random number, you could use a variety of functions to get a value in between the two numbers. If you have semi-uniform distribution, the modulus function would work nicely. In that case, your random number would simply be lowerBound + (System.ByteConverter.ToInt32(arr, start_index) % (upperBound - lowerBound)).

How can i generate unique number as format XXXX-XXXX-XXXX using c# [closed]

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 5 years ago.
Improve this question
I want to generate number as format above and I try to use UUID() and GUID() but it is not what I want(difference format and it is hexadecimal not number)
anyone have any idea or logic to do it?
The simplest way to generate 1000000000000 unique numbers is just an ever-increasing sequence:
long i = 42; // whatever
string key = $"{i / 100000000:0000}-{(i / 10000) % 10000:0000}-{i % 10000:0000}";
The next time, increase i before generating the key.

how to convert decimal value into int in c# [closed]

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 years ago.
Improve this question
I have decimal values in the database. I want to show the decimal value in it. How to convert decimal values into it.
int vatprice;
dt.Load(cmd.ExecuteReader());
vatprice = Convert.ToInt32(dt.Rows[0][5].ToString());//getting error here
As per your requirement, round decimal up to 2 place using following way :
1) Use Math.Round().
var vatprice= Convert.ToDecimal(Math.Round(dt.Rows[0][5], 2));
2) second way is
var vatprice=Convert.ToDecimal(dt.Rows[0][5].ToString ("#.##"));

Regular expression to scan signed fixed-length decimal [closed]

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
How to write a regular expression to scan whether input is a signed decimal or not?
e.g:
-1.234567
-.1234567
123456789
1.2345678
1234567.8
-1234.567
Input length must be 9.
Why are you using RegEx? There are better methods to determine if a string is signed or not.
Use decimal.TryParse() and Math.Sign() to get your answer.
string input = "-1.2342";
decimal decValue;
bool isDecimal = decimal.TryParse(input, out decValue);
if (isDecimal)
{
int signValue = Math.Sign(decValue);
}
else
{
throw new Exception("Not a valid decimal!");
}
You could do it the hard way:
([-\d]\d\.\d\d\d\d\d\d|[-\d]\d\d\.\d\d\d\d\d|[-\d]\d\d.\d\d\d\d|...)
Basically this is any of the forms you listed OR-ed. This is quite tedious, but right now I can't think of any other way that will work for all possible inputs.

Categories