ln.json/ln.json/JSONNumber.cs

78 lines
2.3 KiB
C#
Raw Permalink Normal View History

2017-11-03 13:13:09 +01:00
using System;
using System.Globalization;
2020-11-18 00:36:18 +01:00
using ln.type;
2019-08-07 23:02:00 +02:00
namespace ln.json
2017-10-26 16:41:14 +02:00
{
2019-08-07 23:02:00 +02:00
public class JSONNumber : JSONValue
{
2019-08-08 00:34:16 +02:00
public Decimal Decimal => decValue;
2019-08-07 23:02:00 +02:00
readonly decimal decValue;
2019-08-29 14:16:55 +02:00
public double AsDouble => (double)decValue;
public long AsLong => (long)decValue;
public int AsInt => (int)decValue;
2019-08-19 14:14:15 +02:00
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) { }
2019-08-07 23:02:00 +02:00
public JSONNumber(int i)
: this((long)i) { }
public JSONNumber(long integer)
: base(JSONValueType.NUMBER)
{
decValue = new decimal(integer);
}
2019-08-08 00:34:16 +02:00
public JSONNumber(uint i)
: this((ulong)i) { }
public JSONNumber(ulong integer)
: base(JSONValueType.NUMBER)
{
decValue = new decimal(integer);
}
2019-08-07 23:02:00 +02:00
public JSONNumber(double doubleValue)
: base(JSONValueType.NUMBER)
{
2019-08-29 14:16:55 +02:00
decValue = doubleValue.ToDecimal();
2019-08-07 23:02:00 +02:00
}
public JSONNumber(decimal decValue)
: base(JSONValueType.NUMBER)
{
this.decValue = decValue;
}
2023-08-17 11:46:46 +02:00
public override JSONValue Clone() => new JSONNumber(this.decValue);
2019-08-07 23:02:00 +02:00
public override string ToString()
{
return decValue.ToString(CultureInfo.InvariantCulture);
}
2023-08-29 21:01:32 +02:00
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;
2019-08-07 23:02:00 +02:00
}
2017-10-26 16:41:14 +02:00
}