string - java division of integers set precision -


i have 2 ints

int = 5324; int b = 5; 

dividing "a" "b" should answer "1064.8". want convert answer string. expecting string "1064.8". , interested in "8" comes after "."

how make sure string contain 1 character after "."

here try:

float answer = ((float) a)/b; string s = answer.tostring(); character result = s.charat(s.length()-1); 

but, not able make sure result same character after "."

you can multiply result 10 digit you're interested in last digit in integer division result. can use % 10 last base 10 digit:

class test {   public static void main(string[] args) {     int = 5324;     int b = 5;      int fixedpointresult = 10*a/b;     int lastdigit = fixedpointresult % 10;      system.out.println(lastdigit);   } } 

this prints 8


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -