Browsing 16016 questions and answers with Jon Skeet

Cannot deserialze the current JSON array into type

I'm trying to deserialize following JSON data : { "bids": [ [ "392031.00000000", "0.00254444" ], [ "390000.00000000", "0.52917503" ], ...
Jon Skeet
people
quotationmark

The JSON you've shown would be represented by: public class OrderBookResponse { [JsonProperty("bids")] public List<List<string>> Bids { get; set; } [JsonProperty("asks")] public List<List<string>>... more 1/23/2018 3:22:44 PM

people

Calculate years, months, days between two dates

I am trying to display the years, months and days between two dates in a C# application that I am creating. (Using console for testing purposes) I'm using NodaTime to achieve...
Jon Skeet
people
quotationmark

The issue is indeed that you're using the "include end date in calculation" in timeanddate.com. The solution isn't to add a day to the period in Noda Time - it's to add a day to the end date before you perform the calculation: Period... more 1/23/2018 2:25:13 PM

people

Change 1.0, 2.0 like float values to 1 , 2 | remove trailing zeros from floating binary point types

I am receiving double values from API call .That method return this kind of values 2.0, 2.344, 2.034, 3.1 etc. If I receive value like 2.0, 4.0, I want to convert them 2, 4....
Jon Skeet
people
quotationmark

Assuming this is a matter of formatting a double value as a String, ensuring there are no trailing insignificant zeroes, I'd advise using a DecimalFormat to control the formatting. For example: import java.text.*; import... more 1/23/2018 8:25:23 AM

people

C#: Exponential Format Specifier

I have a double number: element.MaxAllowableConcLimitPpm = 0.077724795640326971; I need to display it as 7.7725e-2 when I try to use...
Jon Skeet
people
quotationmark

You have to use a custom numeric format string - standard numeric format strings always have at least three digits in the exponent. Example with a custom string: using System; public class Test { static void Main() { ... more 1/22/2018 8:28:04 PM

people

Determine file age in days

Given a Path, I would like to determine the file's age based on last modified time. I know how to get the last modified time: FileTime t = Files.getLastModifiedTime(path); I...
Jon Skeet
people
quotationmark

I wouldn't use LocalDate.now() - that depends on your current time zone. A file's age in days of elapsed time can be computed in a time zone neutral manner. Instead, convert the FileTime to an Instant via toInstant, and then you can find... more 1/22/2018 5:20:30 PM

people

Is floating point arithmetic stable?

I know that floating point numbers have precision and the digits after the precision is not reliable. But what if the equation used to calculate the number is the same? can I...
Jon Skeet
people
quotationmark

But what if the equation used to calculate the number is the same? can I assume the outcome would be the same too? No, not necessarily. In particular, in some situations the JIT is permitted to use a more accurate intermediate... more 1/22/2018 2:55:31 PM

people

How to Speed Up this Encryption Method C# Filestream

I have encryption method that runs incredible slowly. It takes around 20 minutes to encrypt several hundred MB of data. I'm not sure if I'm taking the right approach. Any...
Jon Skeet
people
quotationmark

While encryption can be slow, I wouldn't expect that to be the issue here. I suspect it's the byte-by-byte IO that's causing unnecessary overhead. The simplest way to fix that is with a judicious call to Stream.CopyTo - and while you're at... more 1/19/2018 10:25:02 PM

people

Can one reduce the number of type parameters?

I find it annoying that one has to specify the types of both Foo and FooFactory in the call to RunTest below. After all, if the test knows the type of the Factory, the type the...
Jon Skeet
people
quotationmark

It's not clear that TFactory has to be a type parameter at all. I'd write this: internal void RunTest<T>(IFactory<T> factory) { T t = factory.Create(); Assert.NotEqual(default(T), t); } Then you can just... more 1/16/2018 6:19:12 PM

people

How to reuse a SimpleDateFormat class object in java to get different formatted answer

We have a Calendar class object: Calendar calendar = Calendar.getInstance(); And we have a SimpleDateFormat object which is formatted like below: SimpleDateFormat dateFormat =...
Jon Skeet
people
quotationmark

Well you can use applyPattern: SimpleDateFormat dateFormat = new SimpleDateFormat("dd"); System.out.println(dateFormat.format(new Date()); // 16 dateFormat.applyPattern("dd-yy"); System.out.println(dateFormat.format(new Date()); //... more 1/16/2018 7:24:03 AM

people

Adding two hex numbers

I am trying to add two hex numbers "920001A" "920001F" BigInteger number1 = BigInteger.Parse("920001A", NumberStyles.HexNumber); BigInteger number2 = BigInteger.Parse("920001F",...
Jon Skeet
people
quotationmark

Both number1 and number2 are negative, as per the documentation: If value is a hexadecimal string, the Parse(String, NumberStyles) method interprets value as a negative number stored by using two's complement representation if its... more 1/15/2018 11:21:26 AM

people