Converting From String to INT saves another value [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have one function in which i take the numbers as string and when i convert the numbers to int they are saved with some other value.
Why is that Please help.
private string DoTheMath()
{
string s = Console.ReadLine();
string[] s1 = s.Split(' ');
int n1 = Convert.ToInt32(s1[0]);
int k1 = Convert.ToInt32(s1[1]);
}
when i input 49 51
int n1 gets value 31
and int k1 gets value 33

Since you are parsing strings to ints, you probably want Int32.Parse:
private string DoTheMath()
{
string s = Console.ReadLine();
string[] s1 = s.Split(' ');
int n1 = Int32.Parse(s1[0]);
int k1 = Int32.Parse(s1[1]);
}

Related

Why double becomes int? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
When I run this code, I get output "System.Double, 3"
namespace NewApp
{
class Program
{
static void Main(string[] args)
{
double a = 1.0;
double b = 2.7;
a = Math.Round(b);
Console.WriteLine(a.GetType() + ", " + a);
}
}
}
Why I see "3", if a is double variable and I supposed to see "3.0"?
It is still double. You are facing with 3 instead of 3.0 because of the way Console.write works. Use this as example
Console.WriteLine(DoubleConverter.ToExactString(a))
That's because you are saying a is Math.Round(b);
Meaning a will be 3
double a = 1.0; // a -> 1.0
double b = 2.7; // b -> 2.7
a = Math.Round(b); // a = 2.7 "rounded up" -> a = 3
Console.WriteLine(a.GetType() + ", " + a); // a is a double and
// the value is 3 (check previous line)
Edit:
About the decimals, if you round up, you get no decimals, so its 3 instead of 3.0 I believe
Math.Round(); returns a value with no decimals

How to set a while loop? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to set my code for counting math games, all math operations are fine except for division. I don't know how to set the while loop correctly. There is a problem with division, such that I would like the division to be residual, so I came up with one method which is given below. It is all in WPF Application. I would like to count only single-digit numbers.
Random number = new Random();
int maxValue = 10;
int total = 0;
int firstNumber = number.Next(1, maxValue);
int secondNumber = number.Next(1, firstNumber);
while (firstNumber % secondNumber != 0);
{
secondNumber++;
}
total = firstNumber / secondNumber;
Why does it still show me the values ​​that have a residual division?
Thank you for any advice
The semi colon at the end of line:
while (firstNumber % secondNumber != 0);
...ends the while loop. The code in the remaining block is executed without any condition (as it in fact is a anonymous block):
{
secondNumber++;
}

Rounding a double not correct output [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to round a number and the expected output isn't correct. Here's what I have tried:
var percent = Math.Round(1.13451, 0);
That above return's 1, but I want it to return 1.13 if the 3rd number is less than 5. If it's >= 5 (the third number) I want to get something like 1.135. I am sure it's something simple I am missing, but not sure.
It appears you are causing confusion because you're using the term "rounding" to describe an operation that is not actually rounding.
I've read your description again, and I can see that you're trying to truncate your values into the highest discrete increment of 0.005 that does not exceed the value.
You can do this as follows:
var percent = Math.Floor(200.0 * x) / 200.0;
Or, if you want it to be more obvious what's happening, this is essentially the same thing:
var increment = 0.005
var percent = Math.Floor(x / increment) * increment;
you have to write it with the amount of decimals you want. var percent = Math.Round(1.13451, 2);
Updated: I think this is the easiest way to do it.
static void Main(string[] args)
{
var percent = 1.13551;
char[] percent1 = percent.ToString().ToCharArray();
if (percent1[4] <= 5)
{
percent = Math.Round(percent, 3);
}
else
{
percent = Math.Round(percent, 2);
}
Console.WriteLine(percent);
Console.Read();
}
Use:
var percent = Math.Round(1.13451, 2);
Inside an if statement

combine two 16 bit integer to 32 bit float value c# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have to Combine two 16 bit integer and trying to convert into 32 bit float value in my service but I am not able to get. Finally I figure out how to do.
Int16 val1 = 0;
Int16 val2 = 16880;
output should be:
30
Int16 val1 = 0;
Int16 val2 = 16880;
var byteval1 = BitConverter.GetBytes(val1);
var byteval2 = BitConverter.GetBytes(val2);
byte[] temp2 = new byte[4];
temp2[0] = byteval1[0];
temp2[1] = byteval1[1];
temp2[2] = byteval2[0];
temp2[3] = byteval2[1];
float myFloat = System.BitConverter.ToSingle(temp2, 0);

Retrieving a value that is stored as MB [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
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));
}

Categories

Resources