val x = 3.14159265
val s = String.format("%.2f", x);
In the Kotlin playground, test this alternative version that is similar to Python:val x = 3.14159265 val s = "%.2f".format(x); // Output: 3.14
fun Double.dollar( ) : String {
return "$%.2f".format(this)
}
fun main( ) {
var amount = 15.9576
println(amount.dollar( ))
}
// Output: $15.96
var animals = listOf("dog", "wolf", "pig", "skunk", "cow");
// filter only keeps list items for which
// the lambda function returns true.
println(animals.filter { it.length == 3 });
// Output: [dog, pig, cow]
// map applies the lambda function to each item
// in the list, forming a new list from the results.
println(animals.map { it.uppercase( ) });
// Output: [DOG, WOLF, PIG, SKUNK, COW]
// forEach applies the lambda function to each
// element of the list like a for loop.
animals.forEach { print("$it ") }
// Output:
dog wolf pig skunk cow
var stateValue = remember { mutableStateOf("abc") }
...
// Usage
val s = stateString.value // <-- Getter
stateString.value = "xyz" // <-- Setter
Try out these alternative formulations:
// Alternative version 1:
var stateValue by remember { mutableStateOf("abc") }
...
// Usage
val s = stateString // <-- Getter
stateString = "xyz" // <-- Setter
// Alternative version 2:
val (value, setValue) = remember { mutableStateOf("abc") }
...
// Usage
val s = value // <-- Getter
setValue("xyz") // <-- Setter
Alternative version 1 seems to be the most popular.
// GroceryItem Class
class GroceryItem(var description : String, var code : Int,
var price : Double, var onSale : Boolean) { }
fun main( ) {
// For the GroceryItem objects, since the methods
// equals, hashCode, and toString are not overridden,
// the methods are called directly from the Any class.
// Create four GroceryItem objects.
var gItem1 = GroceryItem("Cabbage", 45678, 1.29, false)
var gItem2 = GroceryItem("Green Beans", 98765, 2.45, true)
var gItem3 = GroceryItem("Cabbage", 45678, 1.29, false)
var gItem4 = gItem2;
// Test Any equals method.
println("${gItem1.equals(gItem2)}")
println("${gItem1.equals(gItem3)}")
println("${gItem2.equals(gItem4)}")
println( )
// Output: false false true
// Test Any hashCode method.
print("${gItem1.hashCode( )} ")
print("${gItem2.hashCode( )} ")
print("${gItem3.hashCode( )} ")
println("${gItem4.hashCode( )}")
println( )
// Output: 122883338 500977346 20132171 500977346
// Test Any toString method.
println("$gItem1")
println("$gItem2")
println("$gItem3")
println("$gItem4")
}
// Output:
GroceryItem@7530d0a
GroceryItem@1ddc4ec2
GroceryItem@133314b
GroceryItem@1ddc4ec2
data GroceryItem(var description : String, var code : Int,
var price : Double, var onSale : Boolean) { }
// Output false true true 1391093775 -733823277 1391093775 -733823277 GroceryItem(description=Cabbage, code=45678, price=1.29, onSale=false) GroceryItem(description=Green Beans, code=98765, price=2.45, onSale=true) GroceryItem(description=Cabbage, code=45678, price=1.29, onSale=false) GroceryItem(description=Green Beans, code=98765, price=2.45, onSale=true)