d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Quick Function
Add Reply New Topic New Poll
Member
Posts: 5,269
Joined: Oct 18 2006
Gold: 21,400.00
Jul 19 2013 11:04am
Paying 75fg because im in a hurry and not great with formatting. I need a Java program that takes a double in the form of (xx.00 or xx.50) where xx is any integer number I need it to return a String.

public String function(double in)
{
turns xx.00 -> (String)xx
turns xx.50 -> (String)xx.5
}

Ex.
(double)10.50 -> (String)10.5
(double)10.00 -> (String)10


Pretty Simple, first valid solution gets the fg. If it isn't done in 6 hours, I'll do it. Thanks.
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Jul 19 2013 11:34am
public String function( double n ) {
return n + "";
}

This post was edited by labatymo on Jul 19 2013 11:34am
Member
Posts: 5,269
Joined: Oct 18 2006
Gold: 21,400.00
Jul 19 2013 11:38am
Quote (labatymo @ Jul 19 2013 10:34am)
public String function( double n ) {
    return n + "";
  }


Doesn't fix the zero at the end.

(double)12.50 -> "12.5" != "12.50"
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Jul 19 2013 11:49am
Quote (xandumx @ Jul 19 2013 01:38pm)
Doesn't fix the zero at the end.

(double)12.50 -> "12.5" != "12.50"


I don't get it. Do you want the trailing zeros or not?


Code
 public String function( double n ) {
   return new DecimalFormat( "#.00" ).format( n );
 }


This post was edited by labatymo on Jul 19 2013 11:53am
Member
Posts: 5,269
Joined: Oct 18 2006
Gold: 21,400.00
Jul 19 2013 02:45pm
No trailing zeros.

5.00 -> "5"
5.50 -> "5.5"
10.00 -> "10"
Member
Posts: 3,386
Joined: May 4 2013
Gold: 1,780.00
Jul 19 2013 03:45pm
Code

import java.text.DecimalFormat;
[...]
String str = new DecimalFormat("#.#").format(10.50);

Member
Posts: 5,269
Joined: Oct 18 2006
Gold: 21,400.00
Jul 19 2013 03:55pm
Quote (nuvo @ Jul 19 2013 02:45pm)
Code
import java.text.DecimalFormat;
[...]
String str = new DecimalFormat("#.#").format(10.50);


That was simple enough lmao. Sending the fg.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll