Ejemplo de función Kotlin a la que se le pasa un parámetro en su firma de tipo Double y devuelve el número con separador de miles y decimales en notación de España (punto como separador de miles y coma como separador decimal).
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//Formatea un número para devolver separador de miles y decimal en ES fun FormatearNumeroSeparadorMiles(numero: Double, moneda: String = "€"): String { try { val formato = NumberFormat.getNumberInstance(Locale("es", "ES")) formato.minimumFractionDigits = 2 formato.maximumFractionDigits = 2 var numFormateado = formato.format(numero) + moneda return numFormateado } catch (e: Exception) { return "0,00" } } |
Ejemplo de uso de la función Kotlin anterior:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
@Composable fun LibroVista(libroVistaModelo: LibroVistaModelo, context: Context) { val state = libroVistaModelo.state if (state.estaCargando) { Box( modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center ) { CircularProgressIndicator() } } LazyColumn( horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.SpaceAround ) { stickyHeader { Text( "Facturas", fontSize = 14.sp ) } itemsIndexed(items = facturaVistaModelo.response) { _, item -> Card( modifier = Modifier .padding(8.dp) .fillMaxSize() .background(Color.White) .clickable { val mensaje = "Se ha pulsado en la factura " + item.codigo.toString() EscribirLog(mensaje = mensaje) MostrarMensaje(context, mensaje, tiempoLargo = true) } ) { Column( modifier = Modifier.padding(10.dp), verticalArrangement = Arrangement.Center, ) { Text( text = "[" + item.codigo.toString() + "] " + item.titulo.toString(), fontSize = 13.sp ) Text( " → Fecha: " + item.fecha_compra?.let { SimpleDateFormat("dd-MM-yyyy").format( it ) }, fontSize = 12.sp ) Text( text = " → Importe: " + item.precio?.let { FormatearNumeroSeparadorMiles( it ) }, fontSize = 11.sp ) } } } } |