ln.json/JSONNumber.cs

58 lines
1.4 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;
}
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;
}
public override string ToString()
{
return decValue.ToString(CultureInfo.InvariantCulture);
}
}
2017-10-26 16:41:14 +02:00
}