Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Math: Move dot

Status
Not open for further replies.

neltan

Programmer
Nov 4, 2002
82
0
0
AU
Dear all,
It there a simple way to do this?
----------------------
double d = 12345;
double e = func(d);
e = 12.345;

Thank you very much!
 
Code:
double d = 12345;
double e = d / 1000;
System.out.println(e); // 12.345

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
<tongue_in_cheek>

I think you'll find this code a lot more succinct - why bother with simple maths when you can use String functions eh ??!

Code:
int dec= 2;
int i = 13245;
char decimalPoint = '.';
String sNum = "" +i;
String pre = sNum.substring(0, dec);
String suf = sNum.substring(dec+1, sNum.length());
String final = pre +decimalPoint +suf;
System.out.println(final);

</tongue_in_cheek>

--------------------------------------------------
Free Database Connection Pooling Software
 
Or you could go character by character:
Code:
String num = "12345";
char[] chars = num.toCharArray();
char sep = '.';
int len = 3;
String final = "";
for (int i=0; i<chars.length; i++)
{
  final += chars[i];
  if (chars.length-final == len)
    final += sep;
}

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
Nice chessbot ... an even more convoluted method !

--------------------------------------------------
Free Database Connection Pooling Software
 
Ok, another way

Code:
public putADotInMyString (String number, int dotPosition) {
javax.swing.text.StringContent sc = new javax.swing.text.StringContent();
sc.insertString(0,number);
sc.insertString(dotPosition,".");
return sc.getString(0,number.length()+1);
}

Cheers.

Dian

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top