indofraiser
indofraiser

Reputation: 1024

Value of Double not correct when converting to Currency

I'm doing C# examples via a video tutorial, I'm looked over and over this code but when I run it I find that the addition of "C" to make the value a currency on line: Console.WriteLine("Car's value: {0,C}", myNewCar.DetermineMarketValue()); creates an error: An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format.

I have checked the last line of code return carValue; which is returning "100.0"

I'm a bit stumped and don't want to skip, being from VB.Net I might kick myself for going code blind!!

Full Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleClasses
{
    class Program
    {
        static void Main(string[] args)
        {
            Car myNewCar = new Car();

            myNewCar.Make = "Mini";
            myNewCar.Model = "Cooper";
            myNewCar.Year = 2010;
            myNewCar.Color = "Blue";


            Console.WriteLine("{0} - {1} - {2}",
                myNewCar.Make,
                myNewCar.Model,
                myNewCar.Color);

            //double marketValueOfCar = determineMarketValue(myNewCar);

            Console.WriteLine("Car's value: {0,C}", myNewCar.DetermineMarketValue());

            Console.ReadLine();
        }

        private static double determineMarketValue(Car _car)
        {
            double carValue = 250.00; 
            //this would usually come from online but this is hardcoded for examples
            return carValue; 
        }
    }

    class Car
    {
        public string Make { get; set; }
        public string Model { get; set; }
        public int Year { get; set; }
        public string Color { get; set; }

        public double DetermineMarketValue()
        {
            double carValue = 250.00;

            if (this.Year > 2015)
                carValue = 200.00;
            else
                carValue = 100.00;

            return carValue; 
        }
    }
}

Upvotes: 0

Views: 100

Answers (5)

Michael D
Michael D

Reputation: 678

The formatting should be {0:C} (instead of {0,C}.

Upvotes: 2

Kimbo
Kimbo

Reputation: 29

You should use

Console.WriteLine(string.Format("Car's value {0:c}",myNewCar.DetermineMarketValue());

Upvotes: -1

user1666620
user1666620

Reputation: 4808

I've checked the documentation - the only thing I can think of is you use a comma instead of a semicolon:

Console.WriteLine("Car's value: {0,C}", myNewCar.DetermineMarketValue());

should be

Console.WriteLine("Car's value: {0:C}", myNewCar.DetermineMarketValue());

https://msdn.microsoft.com/en-us/library/586y06yf(v=vs.110).aspx

Upvotes: 2

Sors
Sors

Reputation: 514

Console.WriteLine("Car's value: {0,C}", myNewCar.DetermineMarketValue());

The error on this line is told you by the Exception: the input string "Car's value: {0,C}" is in a bad format.

If you check the Composite Formatting Syntax on msdn you will see that the error is at {0,C} which instead should be {0:C}

Upvotes: 1

amit dayama
amit dayama

Reputation: 3326

use the following line

Console.WriteLine("Car's value: {0:C}", myNewCar.DetermineMarketValue());

and in your function use decimal:

private static decimal determineMarketValue(Car _car)
    {
        decimal carValue = 250.00; 
        //this would usually come from online but this is hardcoded for examples
        return carValue; 
    }

Upvotes: 2

Related Questions