-3

I have to manage doubles that go in a range of:

from 0,000001 to 999.999,999999

What I want to achieve is to reduce this number taking in account only the 6 more important digits.

This means that for the number: 100.000,123 I want to get 100.000

Whereas for the number: 12,123456 I want to get 12,1234

And for 0,123456 I want to get 0,123456

And for 0'000001 I want to get 0'000001

Any clever procedure to get to it? Thanks!

azro
  • 53,056
  • 7
  • 34
  • 70
Mr.Eddart
  • 10,050
  • 13
  • 49
  • 77
  • `((long)(myDouble * 1000000))/1000000d ?` – Bohemian Feb 19 '18 at 16:21
  • 1
    Hmm, why would you want to though? You're essentially wanting to remove some of the accuracy of a double? – Thomas Cook Feb 19 '18 at 16:22
  • 1
    Your question only contains requirements - it is not showing any efforts from your side to solve this problem yourself. Please add your attempts to this questions - as this site is not a free "we do your (home)work" service. Beyond that: please turn to the [help center](https://stackoverflow.com/help) to learn how/what to ask here. Thanks – azro Feb 19 '18 at 16:24
  • @Bohemian, your solution does not do the job I think... ThomasCook, exactly, I want to drop some accuracy, for presentation reasons. azro, I dont agree with your perspective. I dont want to bias the responses with my failed attempts. – Mr.Eddart Feb 19 '18 at 16:28
  • I guess you don't really want to drop some accuracy on a double, you just want to format the output string for presentation reasons. [Have a look here](https://stackoverflow.com/questions/12806278/double-decimal-formatting-in-java) – Oneiros Feb 19 '18 at 16:41
  • @Oneiros, exactly, but numberFormat is not doing the specific job I am looking for. – Mr.Eddart Feb 19 '18 at 16:45
  • Just convert your double in a string and take the first 7 characters – Oneiros Feb 19 '18 at 16:49
  • Yes, I did another question because this has been downvoted, I was looking for something more elegant and performing: https://stackoverflow.com/questions/48870610/performing-way-to-limit-double-accuracy – Mr.Eddart Feb 19 '18 at 16:51
  • 2
    Possible duplicate of [Performing way to limit double accuracy](https://stackoverflow.com/questions/48870610/performing-way-to-limit-double-accuracy) – Spektre Feb 19 '18 at 20:29
  • @edutesoy Why did you open a duplicate question rather than improving this one? You've been around SO long enough to know where the edit button is. – beaker Feb 20 '18 at 15:44

1 Answers1

1

I'm sure there's some clever way to use regex for this, but here's one inefficient way that it can be done:

double d = ...

String s = Double.toString(d);

s = s.substring(0, Math.min(s.startsWith("0") ? 8 : 7, s.length()));

d = Double.parseDouble(s);

It should be possible with the use of existing formatters, which I'll look into shortly and update this answer if I find anything.

Jacob G.
  • 28,856
  • 5
  • 62
  • 116
  • Thanks Jacob, as this question has been downvoted and critizised, I did another one here: https://stackoverflow.com/questions/48870610/performing-way-to-limit-double-accuracy with more accurate description. – Mr.Eddart Feb 19 '18 at 16:49
  • 3
    A downvoted question isn't an excuse to open another :P – Jacob G. Feb 19 '18 at 16:54