2

Do you know any way to easily change every double to float in a source file in eclipse (java)? I.e. how do I change

double a = 123.45

to

float a = 123.45f

I figured out the renaming double to float bit (whoa!), but how to add the f's without having to go through it manually?

Aert
  • 1,989
  • 2
  • 15
  • 17

3 Answers3

6

A regular expression-based search and replace might save you. Search for

double\s+(\w+)\s*=\s*([\-\d.e]+)\s*;

and replace with

float $1 = $2f;

This will take care of literals; you may also wish to replace other kinds of expressions, adding a cast operator. Once you are done with literals, use a similar regex:

double\s+(\w+)\s*=\s*(.+)\s*;

and replace with

float $1 = (float) $2;

Definitely far from foolproof, but it may save you a lot of time.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

Float.parseFloat(String.valueOf(123.45D)); Hehe

Sw4Tish
  • 202
  • 4
  • 12
-1

You can cast it

Double d = 1.0
float f = (float)d
Maciej Cygan
  • 5,351
  • 5
  • 38
  • 72