Updated on 2026-08-14
This commit is contained in:
parent
3ad192c433
commit
93349d80f4
18 changed files with 247 additions and 42 deletions
|
|
@ -0,0 +1,27 @@
|
|||
package com.tangem.core.ui.utils
|
||||
|
||||
/**
|
||||
* Formats input [String] for InputField, to remove wrong symbols, letters etc
|
||||
* Use [decimals] for cut this number symbols after floating point
|
||||
*
|
||||
* Example (with 8 decimals):
|
||||
* input string - ab123.46377372ab53
|
||||
* result string 123.46377372
|
||||
*/
|
||||
fun getValidatedNumberWithFixedDecimals(text: String, decimals: Int): String {
|
||||
val filteredChars = text.filterIndexed { index, c ->
|
||||
c.isDigit()
|
||||
|| (c == '.' && index != 0 && text.indexOf('.') == index)
|
||||
|| (c == '.' && index != 0 && text.count { it == '.' } <= 1)
|
||||
}
|
||||
// If dot is present, take first 3 digits before decimal and first decimals digits after decimal
|
||||
return if (filteredChars.count { it == '.' } == 1) {
|
||||
val beforeDecimal = filteredChars.substringBefore('.')
|
||||
val afterDecimal = filteredChars.substringAfter('.')
|
||||
beforeDecimal + "." + afterDecimal.take(decimals)
|
||||
}
|
||||
// If there is no dot, just take all digits
|
||||
else {
|
||||
filteredChars
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue