Why does a String variable annoted by Spring @Value get a float value?

lily :

I have a springbok project, in the application.yml file, there is a value

account-number: 060018242251

and in a Bean, we have

@Value('${account-number}')
String account

this account variable is assigned with value of "6.0018242251E10" instead of "060018242251", what is wrong and how to deal with it?

MattDiMu :

When parsing the YAML-File, the corresponding parser (in your case probably SnakeYAML) will already determine the data type. As it's too large for an Integer, it will treat it as Double.

When injecting the value, Spring realizes, that you'd like to have a String, but only a Double Value exists. Therefore the toString()-Method is called and you receive it in the mentioned notation.

If you want the value to be a String at the time the YAML is parsed, you should use quotes.

Btw @Prashant The octal notation doesn't affect YAML (or SnakeYAML), the leading 0 is simply ignored.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=307069&siteId=1