Browsing 7239 questions and answers with Jon Skeet

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

Convert datetime string in one timezone to another using offset

I have a datetime string "2018-01-15 01:16:00" which is in EST timezone. I want to convert this into another timezone dynamically using the UTC offset. My javascript code passes...
Jon Skeet
people
quotationmark

The immediate problem is this line: OffsetDateTime offsetDate = OffsetDateTime.of(utcZonedDateTime.toLocalDateTime(), offset); That's saying you want the same local date/time, but with the specified offset. That changes which instant in... more 1/15/2018 7:44:38 AM

people

What is difference between of listofIntegers.add(ValueOf(50)); and listofIntegers.add(50); in Java

What is the difference between these 2 codes: Arraylist<Integer> listofIntegers = new Arraylist<Integer>(); listofIntegers.add(666); System.out.println("First Element...
Jon Skeet
people
quotationmark

The boxing conversion uses Integer.valueOf implicitly, so there's no difference between the two. For example, consider this code: public static void main(String[] args) { Integer x = 100; Integer y = Integer.valueOf(100); } The... more 1/14/2018 4:18:42 PM

people