val userInput:Int = -0
fun costumPrint(x:Int){ println(x) } // = 0 <- not i want , -0 <-is what i want
how can i print/get exatly what in userInput (with negative sign) but keep using integer as parameter
Kotlin (JVM, and generally most) Integers, do not have positive and negative zero.
Some other people have noted Strings, but maybe a Float or Double would suit your use case, as those have 2 values of 0. Note that that would be very hacky and I discourage you, so probably just go for strings
Related
I can easily read 2e15 as "two quadrillion" at a glance, but for 2000000000000000 I have to count the zeroes, which takes longer and can lead to errors.
Why can't I declare an int or long using a literal such as 2e9 or 1.3e6? I understand that a negative power of 10, such as 2e-3, or a power of 10 that is less than the number of decimal places, such as 1.0003e3, would produce a floating point number, but why doesn't Java allow such declarations, and simply truncate the floating-point part and issue a mild warning in cases where the resulting value is non-integral?
Is there a technical reason why this is a bad idea, or is this all about type-safety? Wouldn't it be trivial for the compiler to simply parse a statement like
long x = 2e12 as long x = 2000000000000 //OK for long
and int y = 2.1234e3 as int y = 2123.4 //warning: loss of precision
It's because when you use the scientific notation you create a floating point number (a double in your example). And you can't assign a floating point to an integer (that would be a narrowing primitive conversion, which is not a valid assignment conversion).
So this would not work either for example:
int y = 2d; //can't convert double to int
You have a few options:
explicitly cast the floating point to an integer: int y = (int) 2e6;
with Java 7+ use a thousand separator: int y = 2_000_000;
Because it's a shortcoming of Java.
(Specifically, there is clearly a set of literals represented by scientific notation that are exactly represented by ints and longs, and it is reasonable to desire a way to express those literals as ints and longs. But, in Java there isn't a way to do that because all scientific notation literals are necessarily floats because of Java's language definition.)
You are asking about the rules on writing a integer literals. See this reference:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
The capability to use scientific notation as an integer literal might make things easier indeed but has not been implemented. I do not see any technical reason that would prevent such a feature from being implemented.
I'm creating a currency application but some of values are like "194.23564" or "1187.7594" so i want to show the user before the "." sign values. How can i make this with Kotlin ?
There is no need for data type conversion before the extraction of the integer part.
You can use substringBefore():
val number = "194.23564"
val intPart = number.substringBefore(".")
If you want the result as an integer number you can use now toIntOrNull(), instead of toInt(), so to avoid an exception in case the initial string has no integer part (like ".015"):
val intPart = number.substringBefore(".").toIntOrNull()
Other than suggested, I would not convert to Float. This is susceptible to rounding errors and may not return the value before the decimal point.
Example:
val num = "0.99999999"
println(num.toFloat().toInt()) // gives 1
Instead, split the string at the decimal point:
val num = "0.99999999"
val split = num.split('.')
println(split[0]) // gives 0
A nice side effect of this implementation is that it even works for integral numbers without a decimal point. If you need the result as an Int, simply call split[0].toInt().
There is no need to use float in this case. If you want to get the value before ".", you need to use int instead of float. When you use float you will get value in points, but when you use int, you will get the value before ","
I'm new for Kotlin. I did search and read the docs but couldn't figure out What the best data type to use in Kotlin for currency. In Java there is BigDecimal. Is there something similar in Kotlin? Thanks in advance.
You can use BigDecimal in kotlin too.
var num1 : BigDecimal? = BigDecimal.ZERO
var num2 = BigDecimal("67.9")
Also you can use Double data type and then you can use toBigDecimal() for convert it to BigDecimal.
For the more details :- https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/to-big-decimal.html
If you're targeting the JVM, then you can use BigDecimal in Kotlin, too (as other answers have said) — in fact, anything that's available for Java can also be used in Kotlin! And often in a more concise and natural way.
For example:
val a = BigDecimal("1.2")
val b = BigDecimal("3.4")
println("Sum = ${a + b}")
println("Product = ${a * b}")
val c = a / -b
That's probably the standard way to store money values right now. If you needed extra features, you could write a class wrapping a BigDecimal along with e.g. a java.util.Currency or whatever else you needed.
(If you're targeting JS or native code, then BigDecimal isn't available, though that may change.)
The reason why it's not good practice to use floats or doubles for storing money values is that those can't store decimal values exactly (as they use floating-point binary), so are only appropriate when efficiency (of storage/calculation) is more important than precision.
If you'll never be dealing with fractions of a penny/cent/&c, then an alternative might be to store an integer number of pennies instead. That can be more efficient, but doesn't apply very often.
(You'll note the example above creates BigDecimals from Strings. This is usually a good idea; if you create them from floats or doubles, then the value will already have been truncated before BigDecimal gets to see it.)
You can use BigDecimal in kotlin, please see below variable
var currency: BigDecimal? = BigDecimal.ZERO
iam allmoust done with my app but iam stuck on this thing , i have two int's
called "positive" and "negative" and when i procces source below it shows 0.0
total = positive + negative;
float rate = positive/total;
rate*=100;
TextView analitycs = (TextView)v.findViewById(R.id.app_scores_analitycs);
analitycs.setText(String.valueOf(rate));
What Victor said is true.
Also you might want to use something different than String.valueOf(rate) to set the text of your text view, because this method can give you an ugly representation of the number.
You should probably use String.format("%.2f", rate) ad tweak that to your needs.
Are positive and total floats/doubles?
If not, then an int/int will give you an int.
The solution would be to cast either positive or negative as a float.
try the following:
float rate = ((float)(positive))/total;
Why is it that when I call a SUM on one of my SQLite columns, it doesnt return a precise answer? Heres what I mean:
Lets say I have 4 rows in my column to sum
row1 8362.82
row2 +18837.42
row3 +7294.12
row4 +73.23
___________________
17567.59
Now these should add up to 17567.59 right? Well my sum returns 17567.6. This may not sound like a big deal but I need accurate decimals, not rounded ones. It rounds further as the numbers get larger too. Can anyone explain a solution to this? Thanks.
Store the values as integers instead of floats.
I had the same roundoff problem, except mine were rounding to integers instead of maintaining one place after the decimal point. I switched to using total() instead of sum().
From the documentation:
The result of total() is always a floating point value. The result of
sum() is an integer value if all non-NULL inputs are integers. If any
input to sum() is neither an integer or a NULL then sum() returns a
floating point value which might be an approximation to the true sum.
This doesn't explain the issue in my case, because my column contained a mix of integer and floating-point values. But I decided to try it because of the difference, and it worked. It also sums all NULLs to 0 instead of NULL which is useful in many cases.