0

I have the following problem trying to store the correct output of this SQL query into a double variable into a Java applcation.

So I have this SQL query:

SELECT sum(anagraficaEdificio.imp_tot_ind - anagraficaEdificio.imp_cof) FROM TID003_ANAGEDIFICIO anagraficaEdificio
INNER JOIN TID002_CANDIDATURA candidatura ON (candidatura.PRG_PAR = anagraficaEdificio.PRG_PAR AND candidatura.PRG_CAN = anagraficaEdificio.PRG_CAN)
INNER JOIN TID001_ANAGPARTECIPA anagPartecipa ON(anagPartecipa.PRG_PAR = candidatura.PRG_PAR)
INNER JOIN anagrafiche.TPG1029_PROVNUOIST provNuovIst ON (provNuovIst.COD_PRV_NIS = anagPartecipa.COD_PRV_NIS)
WHERE FLG_GRA=1

Performing this query directly on my database (using its client) I obtain this correct numeric result: 36481888,47

Ok, so I have implemented it into a Java application (using SpringDataJpa, but I think that this is not important, because it is performed as SQL native query) by:

@Query(value = "SELECT sum(anagraficaEdificio.imp_tot_ind - anagraficaEdificio.imp_cof) FROM TID003_ANAGEDIFICIO anagraficaEdificio "
         + "INNER JOIN TID002_CANDIDATURA candidatura ON (candidatura.PRG_PAR = anagraficaEdificio.PRG_PAR AND candidatura.PRG_CAN = anagraficaEdificio.PRG_CAN) "
         + "INNER JOIN TID001_ANAGPARTECIPA anagPartecipa ON(anagPartecipa.PRG_PAR = candidatura.PRG_PAR) "
         + "INNER JOIN anagrafiche.TPG1029_PROVNUOIST provNuovIst ON (provNuovIst.COD_PRV_NIS = anagPartecipa.COD_PRV_NIS) "
         + "WHERE FLG_GRA=1", nativeQuery = true)
double getImportoFinanziabileNazionale();

that perform the original query.

The problem is that when I call this method by:

double risultato = tid003AnagedificioRepository.getImportoFinanziabileMiurNazionale();

the value stored into the risultato variable is not 36481888,47 (the correct one) but I obtain this value 3.648188847E7.

Why it contains an E inside it? Is something related to an exponensial notation or something like this? How can I obtain the oringial 36481888,47 value returned by my query into my Java application? Is it related to the use of double?

What am I missing? How can I fix this issue?

radoh
  • 4,554
  • 5
  • 30
  • 45
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • 2
    if this is a curreny, no doubles are not prefered for currencies, due to the precission loss. you might want to go with [BigDecimal](https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html) – SomeJavaGuy Mar 10 '16 at 12:58
  • 1
    `3.648188847E7` is nothing else than the default String representation of your double. If you need another representation as a String, you may want to look at what `String.format` can do : http://stackoverflow.com/questions/703396/how-to-nicely-format-floating-numbers-to-string-without-unnecessary-decimal-0 – Arnaud Mar 10 '16 at 12:59
  • 4
    The value is correct - the display of the value is not what you expect. Trust me, it's fine. It's simply formatting the value being displayed, but feel free to use it in calculations, or use String format to show it differently. – Kieveli Mar 10 '16 at 12:59
  • 1
    Check this out http://stackoverflow.com/questions/16098046/how-to-print-double-value-without-scientific-notation-using-java – soban Mar 10 '16 at 13:01

1 Answers1

1

It has an E because it's written in a different format than you are expecting. It's the same number. 36481888,47 (your number) is the same number as 3.648188847E7. The E is for "Exponent", or power of 10 in this case. You can print it the way you prefer.You didn't post your Java code: look up printf or another formatting function so see how you can print it according to your preference. http://web.cerritos.edu/jwilson/SitePages/java_language_resources/Java_printf_method_quick_reference.pdf

nicomp
  • 4,344
  • 4
  • 27
  • 60