473,383 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

truncating numbers

Hello,

when i declare the variable:

double num = 1.928761821237;

if i want to display it with proper formatting, i will use a printf and set the number of digits i will display. Assuming i now want to truncate this number in memory to

num = 1.92876;

is there any way to do this and disregard the numbers that follow?

thanks
Jan 26 '09 #1
4 5560
r035198x
13,262 8TB
If you are going to be changing the scale then why not use the BigDecimal class and set the scale to the required digits.
Jan 27 '09 #2
chaarmann
785 Expert 512MB
Four additional ways:
a)You can use printf to convert it to a string (with the exact number of wanted digits given in printf format string), and then convert it back from String to Number. You can also use DecimalFormat.format() class for it.
b) You can convert it to a String with String.valueOf(num), cut off the last digits with String.substring(), and then convert it back with Double.parseDouble()
c) You can multiply the number with a power of 10. If you want to trim away n digits, then you would multiply with Math.pow(10, n). In your case it's 10000. Then you can cut off the last digits with Math.round() or Math.rint() or Math.floor(), whatever suits you best. Then you would divide the result by the number you multiplied before (in your case 10000). But sometimes you get inaccurate results depending on your math processor hardware. So for high accuracy, but less speed, use functions of StrictMath package.
d) best solution: download Jakarta Commons library MathUtils.
Then use MathUtils.round(num, n) , which rounds the given value "num" to the specified number "n" of decimal places.
Jan 27 '09 #3
thanks to both r035198x and chaarmann i'll try both methods and see which is simpler for me to use!

thanks again!
Jan 28 '09 #4
JosAH
11,448 Expert 8TB
Also read the link about floating point arithmetic in the first (sticky) article in this group.

kind regards,

Jos
Jan 28 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Coral G via AccessMonster.com | last post by:
Using Access 2000 I need to truncate the last few characters of a string based on the presence of a dash (-). This dash is not always present and can be followed by a variable number of numbers...
4
by: qazmlp | last post by:
Can anybody comment(& suggest improvements) on the following implementation that is for truncating the character buffer contents to the desired length? Truncating char array void...
5
by: VISHNU VARDHAN REDDY UNDYALA | last post by:
Hello, Can someone over here help me in truncating a float variable. I mean if PI=3.14159 ...How can I get to read the first two or first three decimal values with out rounding them. Any...
1
by: Jitesh Sinha | last post by:
Hi, I am running Windows 2003/ IIS 6.0. I was stuck with rather a abnormal behaviour of System.Web.mail class. It was truncating the message body after 3,071 character. The code i was testing...
5
by: GaryB | last post by:
I have a database field that is the product of a multiply in a sql statement and it sometimes has 5 or more decimal places. I want to truncate beyond the 2nd decimal place(###.##). I figured a...
3
by: rangermccoy | last post by:
Hello there, What are the best php/c libraries for handling media including images, video, and music? I would like to manipulate media dfiles, including watermarking, thumbnailing,...
1
by: sconnors | last post by:
I am currently working on an application that runs on Windows XP SP2 with .NET 2.0, Linux 2.4.19-16mdkenterprise and Solaris 5.9. The language is C++. When I compare the output numbers, Windows...
5
by: Alex Leach | last post by:
I need to check whether a string of letters contains any numbers at the end of the string, and if so, remove all numbers. There won't ever be numbers at the beginning or middle of the string. Any...
1
by: Serenityquinn15 | last post by:
Hi! I have a little trouble about in truncating numbers. What i want to do is to get the two decimal places. here's the example: when i try to get the half of 20500.39 it shows like...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.