Use floating point or comma?

Asked

Viewed 71 times

-3

I would like to know the difference of a mathematical operation with a number with a comma and another with a dot.

Example:

var altura = 1,83

Or

var altura = 1.83

Which one is right?

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

1

Neither line is correct because both lines are missing ; to finalize the statement which is a declaration of a variable and assignment of a value of the type double.

IS double because it uses var which indicates to the compiler that the type must be defined by the value it is assigning to the variable being declared. And the assigned value is a literal of type double because it has been agreed that whenever a numeric literal has a decimal part and no suffix to indicate the type then the type is double.

The first is more wrong because this syntax with , is not accepted, just write, try to compile and will result in error. Just as the programming language is all in English, the standard adopted for decimal separator is the ., and not what we are used to here in Brazil.

The comma is used to separate expressions in some contexts, but this is not separating expressions. If the compiler accepted this as a list of expressions using the comma, then the latter would be prevalent and discard the others, so the value assigned would be 83, and the type of the variable would be int for not having decimal part. But by "luck" the compiler does not even accept it.

  • Thank you very much Maniero for the explanation! Hug

  • @JJB see on [tour] the best way to say thank you when a reply met what he wanted. You can mark with accepted and vote on the reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.