-1
Console.WriteLine("Please enter a number");
int num = int.Parse(Console.ReadLine());
int num1 = int.Parse(Math.Floor(num));
int num3 = int.Parse(num - num1);

Console.Write("shekel: "); 
Console.WriteLine(num1);

Console.Write("agorot: "); 
Console.WriteLine(num3);

I'm getting an error in line 3:

Severity Code Description Project File Line Error CS0121 The call is ambiguous between the following methods or properties: 'Math.Floor(decimal)' and 'Math.Floor(double)

And in line 4:

And Severity Code Description Project File Line Error CS1503 Argument 1: cannot convert from 'int' to 'string'

I'm supposed to make a program that recieves a number (such as 10.22) and in one message and then in another message say what's the decimal number and what's the number after the dot.

What am I doing wrong?

wingerse
  • 3,670
  • 1
  • 29
  • 61
John doe
  • 7
  • 2
  • 3
    What do you expect `Math.Floor` to do when you pass it an integer? Hint: if you're expecting the user to enter "10.22" then you *don't* want to parse that as an `int`. I'd use `decimal`, myself... – Jon Skeet Oct 08 '15 at 20:18
  • Why are you parsing `Math.Floor`, `num - num1` when they are already integers? – Ron Beyer Oct 08 '15 at 20:19
  • @Johndoe please when pasting / posting code.. post all relevant code also in your spare time read up on some `C# basics Tutorials` – MethodMan Oct 08 '15 at 20:24
  • By definition, integers are whole numbers. If you're taking a decimal from the user, you don't want to store it in an `int`. Also, if you mouse-over `Console.WriteLine()` you can see it expects a `string` as an argument, not an `int`. Convert your variables to strings in the `Console.WriteLine()` call. – sab669 Oct 08 '15 at 20:30
  • `Console.WriteLine` can also accept an int. It's not a problem. – wingerse Oct 08 '15 at 20:42

2 Answers2

0

You should do something like this:

decimal number = Decimal.parse(Console.ReadLine());
int numberInteger = int.Parse(Math.Floor(number));
decimal numberAfterDot = number - numberInteger;

Also, keep in mind that some numbers are written in this form: 10.22 and some like this: 10,22

Theo
  • 154
  • 1
  • 6
  • I get this error in the second line you wrote: "Severity Code Description Project File Line Error CS1503 Argument 1: cannot convert from 'decimal' to 'string'" – John doe Oct 08 '15 at 20:30
0

First of all, if you want a decimal(Mathematically) number , why do you parse it as an int? You want to parse it as a decimal/double/float (They are different see this question), but I'd use double anyway.

You can do something like :

Console.WriteLine("Please enter a number");
decimal num = decimal.Parse(Console.ReadLine());
decimal firstPart = Math.Truncate(num);
decimal secondPart = num - firstPart;

Console.Write("shekel: ");
Console.WriteLine(firstPart);

Console.Write("agorot: ");
Console.WriteLine(secondPart);

If you want to get rid of the integral 0 in the secondPart, you can convert it to a string and slice it.

Community
  • 1
  • 1
wingerse
  • 3,670
  • 1
  • 29
  • 61
  • Oh, thank you for you answer, but we haven't learned "Math.Truncate" And I couldn't find it in our learning books either, what does it do? – John doe Oct 08 '15 at 20:38
  • Well, it's nothing difficult. It just calculates the integral part of a specified double or decimal number. See http://stackoverflow.com/questions/14/difference-between-math-floor-and-math-truncate for the difference between `Floor` and `Truncate` – wingerse Oct 08 '15 at 20:40
  • So `Truncate` is the right method you want to use here. – wingerse Oct 08 '15 at 20:41