Working with Decimals and Integers [duplicate] - c#

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 5 years ago.
I'm sure this is a stupid question but I'm a newbie!
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
var myInt = ((1000/2048) * 1536);
Console.WriteLine(myInt);
}
}
}
Output is:
0
Can someone tell me how to get the correct number (750)?

By default, 1000 and 2048 are int, therefore the result will be treated as an int.
You have to cast them to double.
Option 1:
var myInt = (((double)1000 / 2048) * 1536);
Console.WriteLine(myInt);
Option 2:
var myInt = ((1000.0 / 2048) * 1536);
Console.WriteLine(myInt);
You should read about value types in C#:
https://www.tutorialspoint.com/csharp/csharp_data_types.htm
https://msdn.microsoft.com/en-us/library/system.double(v=vs.110).aspx

Related

Array returns System.Int32[]? [duplicate]

This question already has answers here:
System.Int32[] displaying instead of Array elements [duplicate]
(4 answers)
Closed 2 years ago.
When I make a function to return an array with the correct results.
Instead of giving me the correct results, I get as result System.Int32[].
Anyone an idea why this is?
class Program
{
static void Main(string[] args)
{
Console.WriteLine(MultiplyByLength(new int[] {2,3,1,0}));
}
public static int[] MultiplyByLength(int[] arr)
{
return arr.Select(x => x * arr.Length).ToArray();
}
}
You need to format it some how. An array doesn't have a ToString() override that knows how you want to format your type (int[]) to a string, in such cases it just returns the type name (which is what you are seeing)
foreach(var item in MultiplyByLength(new int[] {2,3,1,0})
Console.WriteLine(item);
or
Console.WriteLine(string.Join(Environment.NewLine, MultiplyByLength(new int[] {2,3,1,0}));
or
Console.WriteLine(string.Join(",", MultiplyByLength(new int[] {2,3,1,0}));

Why does this return 0? [duplicate]

This question already has answers here:
No overflow exception for int in C#?
(6 answers)
Closed 4 years ago.
using System;
namespace test_warmup
{
class Program
{
static void Main(string[] args)
{
int test = -1110835200;
test = test * 1700397056;
Console.WriteLine(test);
}
}
}
-1110835200 * 1700397056 = -1888860903781171200
Displayed in hex, that's -0x1a3694fc_00000000
In C#, int is only 32-bits, so the result is truncated to just 0.
In other words, the result of that multiplication is too large to fit in the variable to which you're assigning it, and the part that fits is all zero.

Char to Int explicit Conversion in C# [duplicate]

This question already has answers here:
Convert char to int in C#
(20 answers)
Closed 9 months ago.
I was trying to learn explicit conversion in c#.
First I explicitly converted string to int
namespace MyfirstCSharpCode
{
class Program
{
static void Main(string[] args)
{
string str = "1234";
int i = Convert.ToInt16(str);
Console.WriteLine(i);
}
}
}
it worked and the result is : 1234
Now I tried to convert char to int
namespace MyfirstCSharpCode
{
class Program
{
static void Main(string[] args)
{
char str = '1';
int i = Convert.ToInt16(str);
Console.WriteLine(i);
}
}
}
And now the result is 49, the ASCII value of 1. How to get the char 1 instead of ASCII value. Please don't be too harsh its my first day in c# ;)..
You hit the overload methods of Conver.ToInt16(char) , and char is a UTF-16 code unit and representing 1 is 49. You can use Int16.TryParse to safe parse the number (need in 16 bit sign number) in order to get the valid integer.
string str = "1"; // use string if your number e.g 122
short number; // convert to 16 bit signed integer equivalent.
if (Int16.TryParse(str, out number))
{
Console.WriteLine(number);
}

The type or namespace name 'Numerics' does not exist in the namespace 'System' [duplicate]

This question already has answers here:
Can't get System.Numerics to work with command-line Mono (mcs) on OS X
(1 answer)
Where is my System.Numerics namespace?
(2 answers)
Closed 9 years ago.
I am solving problem 25 on Project Euler. I wrote a code for it in C#. However in this case I needed to use "BigInt", since Int64 wasn't big enough to hold the number. But, when I put using System.Numerics;, it gives an error message during compiling (the message in title). Why is this? I am using Mono 2.10.9.
My code:
using System;
using System.Numerics;
public class Problem_25{
static BigInt fib(int n){
double Phi = (1+Math.Sqrt(5))/2;
double phi = (1-Math.Sqrt(5))/2;
BigInt an = Convert.BigInt((Math.Pow(Phi, n)-(Math.Pow(phi, n)))/Math.Sqrt(5));
return an;
}
static void Main(){
int i = 100;
int answer = 0;
string current_fn = "1";
while(true){
current_fn = Convert.ToString(fib(i));
if(current_fn.Length == 1000){
answer = i;
break;
}
else{
i++;
}
}
Console.WriteLine("Answer: {0}", answer);
}
}
You need to add a reference to System.Numerics.dll.
mcs -r:System.Numerics.dll main.cs
man mcs should do you the honours.
Depending on your mono version you might want to use dmcs and/or upgrade.
Oh, and it's BigInteger, not BigInt

How do I convert a string to an enum value to an integer? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I Convert a string to an enum in C#?
Enums returning int value
I have declare an enumeration:-
public enum Car
{
SELECT = 0,
AUDI = 1,
NISSAN = 2,
HONDA = 3,
LINCOLN = 4
}
Now I need the int value of enum where it matches:-
private int GetCarType(string CarName)
{
foreach(var item in Enum.GetNames(typeof(Car))
{
if (item.ToLower().Equals(CarName.ToLower()))
//return int value of Enum of matched item; ???????
}
Result expected:-
int i = GetCarType(CarName); //suppose CarName is AUDI, it should return 1;
Console.write(i);
Result :- 1
How will I get value of enum? And better coding practice.
If you are converting a string to an enum, you should use Enum.Parse rather than iterating over the names.
Then just cast to an integer:
var iAsInteger = (Int32)i;
var result = (int)System.Enum.Parse(typeof(Car), carName)
http://msdn.microsoft.com/en-us/library/essfb559.aspx
This replaces your GetCarType function. You no longer have to iterate over the enum names.
simply cast your enum to int like
int i=(int)Car.Audi;
this will give you 1
int i=(int)Car.Select;
this will give you 0
int i = (int) Enum.Parse(typeof(Car), "AUDI");

Categories

Resources