ln.json/ln.json/JSONNumber.cs

78 lines
2.3 KiB
C#

using System;
using System.Globalization;
using ln.type;
namespace ln.json
{
public class JSONNumber : JSONValue
{
public Decimal Decimal => decValue;
readonly decimal decValue;
public double AsDouble => (double)decValue;
public long AsLong => (long)decValue;
public int AsInt => (int)decValue;
public override object ToNative()
{
if (Decimal.Ceiling(decValue).Equals(Decimal.Floor(decValue)))
return (long)decValue;
return (double)decValue;
}
public JSONNumber(byte i)
: this((long)i) { }
public JSONNumber(sbyte i)
: this((long)i) { }
public JSONNumber(short i)
: this((long)i) { }
public JSONNumber(ushort i)
: this((long)i) { }
public JSONNumber(int i)
: this((long)i) { }
public JSONNumber(long integer)
: base(JSONValueType.NUMBER)
{
decValue = new decimal(integer);
}
public JSONNumber(uint i)
: this((ulong)i) { }
public JSONNumber(ulong integer)
: base(JSONValueType.NUMBER)
{
decValue = new decimal(integer);
}
public JSONNumber(double doubleValue)
: base(JSONValueType.NUMBER)
{
decValue = doubleValue.ToDecimal();
}
public JSONNumber(decimal decValue)
: base(JSONValueType.NUMBER)
{
this.decValue = decValue;
}
public override JSONValue Clone() => new JSONNumber(this.decValue);
public override string ToString()
{
return decValue.ToString(CultureInfo.InvariantCulture);
}
public static implicit operator Decimal(JSONNumber j) => j.decValue;
public static implicit operator float(JSONNumber j) => (float)j.decValue;
public static implicit operator double(JSONNumber j) => (double)j.decValue;
public static implicit operator int(JSONNumber j) => (int)j.decValue;
public static implicit operator long(JSONNumber j) => (long)j.decValue;
public static implicit operator uint(JSONNumber j) => (uint)j.decValue;
public static implicit operator ulong(JSONNumber j) => (ulong)j.decValue;
}
}