Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Manipulating numbers

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Given a number x and a number of decimal places y are there any library methods than can produce the following results

x y result
1 3 0.001
10 3 0.010
100 3 0.100

So basically formatting the given number with the given decimal place value. I thought there would be something to do this in the API but haven't found anything. I want to avoid having to write my own routine if possible

Thanks in advance
Jack
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd have to compute a value

double v = x * Math.pow(10, -y);

and then display it using java.text.DecimalFormat:

DecimalFormat f = new DecimalFormat();
f.setMinimumFractionDigits(y);
f.setMaximumFractionDigits(y);
System.out.println(v);

There's no single routine that does this rather unusual manipulation.
 
I think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!