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,869
  • 2
  • 14
  • 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
  • 188,298
  • 27
  • 302
  • 416
0

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

Sw4Tish
  • 202
  • 3
  • 12
-1

You can cast it

Double d = 1.0
float f = (float)d
Maciej Cygan
  • 5,131
  • 4
  • 34
  • 65