d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Only Display Decimals? > Decimalformat Maybe?
Prev12
Add Reply New Topic New Poll
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Oct 14 2014 10:21pm
Quote (carteblanche @ Oct 15 2014 03:31am)
otherwise, cast to string, grab what's right of the decimal.


just do this, seems easy enough and it will work with no hassle
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 15 2014 12:34am
Quote (carteblanche @ Oct 14 2014 09:31pm)
i would double check the decimal format. otherwise, cast to string, grab what's right of the decimal.


Code
irb(main):001:0> number = 921.29748127632
=> 921.29748127632
irb(main):004:0> puts number.to_s[number.to_s.index('.') + 1..-1].to_i
29748127632
=> nil


Cast the number to a string, find the index of the decimal, copy the rest of the string from the right of the decimal, convert this captured string back to an integer.

This post was edited by AbDuCt on Oct 15 2014 12:35am
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Oct 15 2014 07:19am
Code
#!/usr/bin/env python

from math import pi

testicles = [3.14, 4444.4444, 333.333, 69.69]

def mayne(numba):
if isinstance(numba, list) or isinstance(numba, tuple):
for e in numba:
ayy = str(e)[str(e).find('.')+1:str(e).find('.')+4]
yield ayy
elif isinstance(numba, float) or isinstance(numba, int):
ayy = str(numba)[str(numba).find('.')+1:str(numba).find('.')+4]
yield ayy
else:
print('ayy lmao wtf cus')


gin = mayne(testicles)
juice = [int(i) for i in gin]
apple = list(mayne(pi))
satin = list(mayne(666.777))

print(testicles)
print(juice)
print(pi)
print(apple)
print(666.777)
print(satin)


Output:

Code
C:\>python -i heh.py
[3.14, 4444.4444, 333.333, 69.69]
[14, 444, 333, 69]
3.141592653589793
['141']
666.777
['777']
>>>


A lot of languages have a libraries already compiled, not only are they better written, but they that can handle what you want. If you want to have fun though you'll try your own way too, at least for the experience. My code is terrible, inconsistent output, different types used, lists, strings, and crap showing but it functions for what is required and can be tweaked with some more time, using a functional style rather than conditional nonsense, and formatting the output better.

Code
>>> from math import pi
>>> print("{:.2f}".format(pi))
3.14


Don't know anything about Java and quite frankly I don't want to think about it because I just ate breakfast, but the proof of concept suggested (string -> find delimiter -> int) will work in any language.
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Oct 15 2014 07:26am
Code
System.out.println((3.42+"").split( "\\." )[1]);
System.out.println((17.0+"").split( "\\." )[1]);
System.out.println((47.151+"").split( "\\." )[1]);


Output:

42
0
151

or as a function
Code
public static String getDecimalPart(double number){
return (number+"").split( "\\." )[1];
}


This post was edited by labatymo on Oct 15 2014 07:29am
Member
Posts: 13,042
Joined: Apr 12 2008
Gold: 0.00
Oct 15 2014 11:52am
Quote (labatymo @ Oct 15 2014 08:26am)
Code
System.out.println((3.42+"").split( "\\." )[1]);
System.out.println((17.0+"").split( "\\." )[1]);
System.out.println((47.151+"").split( "\\." )[1]);


Output:

42
0
151

or as a function
Code
public static String getDecimalPart(double number){
    return (number+"").split( "\\." )[1];
  }


I believe this is what we were learning in class, but I missed that lecture :(
Thanks for the heads up

Thanks for everyones helps as well!
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll