min Length
Начиная с Kotlin: 2.2
var minLength: Int
Задаёт значение NumberHexFormat.minLength создаваемого формата, 1 по умолчанию.
Значение должно быть положительным.
Подробнее о влиянии параметра формата на результаты форматирования и разбора см. в описании NumberHexFormat.minLength.
Выбрасывает исключение
если этому свойству присвоено неположительное значение.
Примеры
import kotlin.test.*
fun main() {
//sampleStart
// By default, minLength is 1 and removeLeadingZeros is false.
println(0x3a.toHexString()) // 0000003a
// Specifying a minLength shorter than the hex representation with removeLeadingZeros set to false.
println(0x3a.toHexString(HexFormat { number.minLength = 4 })) // 0000003a
// Specifying a minLength shorter than the hex representation with removeLeadingZeros set to true.
val shorterLengthFormat = HexFormat {
number.removeLeadingZeros = true
number.minLength = 4
}
println(0x3a.toHexString(shorterLengthFormat)) // 003a
println(0xff80ed.toHexString(shorterLengthFormat)) // ff80ed
// Specifying a minLength longer than the hex representation.
// removeLeadingZeros is ignored in this case.
val longerLengthFormat = HexFormat {
number.removeLeadingZeros = true
number.minLength = 12
}
println(0x3a.toHexString(longerLengthFormat)) // 00000000003a
// When parsing, minLength is ignored.
println("3a".hexToInt(longerLengthFormat)) // 58
// The number of hex digits can be greater than what can fit into the type.
println("00000000003a".hexToInt()) // 58
println("0000ffffffff".hexToInt()) // -1
// But excess leading digits must be zeros.
// "000100000000".hexToInt() // will fail with IllegalArgumentException
//sampleEnd
}
© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.text/-hex-format/-number-hex-format/-builder/min-length.html