14

I want to do an operation like this : if the given float numbers are like 1.0 , 2.0 , 3.0 , I want to save them to database as integer (1,2,3 ), if they are like 1.1 , 2.1 , ,3.44 , I save them as float. what's the best solution for this problem using java ? The corresponding field in database is type of varchar.

2
  • This is really confusing; are you asking how to use Float.toString(val)? Commented Feb 2, 2010 at 9:24
  • Are you sure that floating point numbers really is the best fit for your problem domain? If you want precision, you should use BigDecimal Commented Feb 2, 2010 at 9:28

5 Answers 5

20

Just try int i = (int) f;.

EDIT : I see the point in the question. This code might work :

 int i = (int) f;
 String valToStore = (i == f) ? String.valueOf(i) : String.valueOf(f);
Sign up to request clarification or add additional context in comments.

3 Comments

This will convert all float values to ints, which isn't really what the original question was asking.
what about float string , dude ? you convert a float to int. this is not what I want.
Actually this works fine : it's not converting all float values to ints. It's storing the int value in "i" just for comparison with the float value. If it's the same (i.e. it ends with .0) then it returns "i", otherwise it returns the original value as a String... @Tony, valToStore is a String. This is what you asked for...
6
String result = "0";
if (floatVar == Math.floor(floatVar)) {
    result = Integer.toString((int) floatVar);
} else {
    result = Float.toString(floatVar);
}

The if-clause checks whether the number is a whole number - i.e. if it is equal to the result of rounding it down to the closest whole value.

But this is very odd requirement indeed, and perhaps you should reconsider the need for such a thing.

8 Comments

He was asking for an integer string, so the line converting to an int should be something along the lines of String str = Integer.tostring((int)floatVar);
You want an 'if' test and a conversion to a String. I think you'll be hard pressed to find a faster solution than Bozho's.
@Tony - why do you think it's not "fast"?
really ? float L ; String LStr = L == (int) L ? (int)L + "" : L +""; primitive is faster than wrapper.
no, it is not ;) The compiler transforms L + "" to Float.toString(L)
|
1

Seems like you want to save Floats with no trailing numbers as Integers, while saving those with significant trailing numbers as Floats. I would rather just save it all as Float to the DB, but it's your question so here's my answer:

/**
   * Method to determine if trailing numbers are significant or not. Significant
   * here means larger than 0
   * 
   * @param fFloat
   * @return
   */
  public static boolean isTrailingSignificant(Float fFloat)
  {
    int iConvertedFloat = fFloat.intValue();// this drops trailing numbers
    // checks if difference is 0
    return ((fFloat - iConvertedFloat) > 0);
  }

This is how you would use this method:

 Number oNumToSave = null;
if (isTrailingSignificant(fFloat))
{
  // save float value as is
  oNumToSave = fFloat;
}
else
{
  // save as int
  oNumToSave = fFloat.intValue();// drops trailing numbers      
}

After that, you can do the database operation using the variable oNumToSave.

Comments

0

Not sure this is the best solution, but you can try to write a method like this :

String convertToString(Float f) {
  if (f.toString().endsWith(".0"))
      return f.intValue().toString();
  else
      return f.toString();
}

Comments

0

Kotlin:

    val mAmount = 3.0
    val intAmount = mAmount.toInt()
    val amountToDisplay = if (intAmount.compareTo(mAmount) == 0) intAmount.toString() else java.lang.String.valueOf(mAmount)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.