-1

Given a number of type double, say d:

How can d be rounded/extracted to it's "most suitable" power of 10?

Example:

0.123 => 0.1

1.234 => 1

12.34 => 10

[At this point, I have not decided which behaviour I want for for example 0.99 (i.e if it should be 0.1 or 0.01 - any solution will do for now.]

I am using this in Java programming, so either some standard library function or just a simple mathematical solution (for any language) will do. (I can think of naive solutions like dividing d by ten and look for the first non zero number, but it feels too ugly)

I am sorry if I am not using the correct terminology in the question, please edit if you can formulate it better.

Joakim
  • 3,224
  • 3
  • 29
  • 53

1 Answers1

3

Compute the base-10 logarithm, using a language of your choice, and round the number up or down according to personal taste.

In Java, you can use Math.log10.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483