Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- namespace Cap06_FormatoParseo
- {
- class Temperatura : IFormattable
- {
- private decimal temperatura;
- public Temperatura(decimal temperatura)
- {
- if (temperatura < -273.15m)
- {
- throw new ArgumentOutOfRangeException(String.Format("{0} is menor que el valor absoluto zero.",
- temperatura));
- }
- this.temperatura = temperatura;
- }
- public decimal Celcius
- {
- get
- {
- return temperatura;
- }
- }
- public decimal Fahrenheit
- {
- get { return temperatura *9/5 + 32; }
- }
- public decimal Kelvin
- {
- get { return temperatura + 273.15m;}
- }
- public override string ToString()
- {
- return this.ToString("G", CultureInfo.CurrentCulture);
- }
- public string ToString(string format)
- {
- return this.ToString(format, CultureInfo.CurrentCulture);
- }
- public string ToString(string format, IFormatProvider formatProvider)
- {
- if (String.IsNullOrEmpty(format))
- {
- format = "G";
- }
- if (formatProvider == null)
- {
- formatProvider = CultureInfo.CurrentCulture;
- }
- switch (format.ToUpperInvariant())
- {
- case "G":
- case "C":
- return temperatura.ToString("F2", formatProvider) + " °C";
- case "F":
- return Fahrenheit.ToString("F2", formatProvider) + " °F";
- case "K":
- return Kelvin.ToString("F2", formatProvider) + " °K";
- default:
- throw new FormatException(String.Format("La cadena de formato {0} no está soportada.",
- format));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement