secondFraction

abstract fun secondFraction(minLength: Int = 1, maxLength: Int = 9)(source)

The fractional part of the second without the leading dot.

When formatting, the decimal fraction will be rounded to fit in the specified maxLength and will add trailing zeroes to the specified minLength. Rounding is performed using the round-toward-zero rounding mode.

When parsing, the parser will require that the fraction is at least minLength and at most maxLength digits long.

This field has the default value of 0. If you want to omit it, use optional.

See also the secondFraction overload that accepts just one parameter, the exact length of the fractional part.

Throws

if minLength is greater than maxLength or if either is not in the range 1..9.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Defining a custom format for the local time
// format the local time as a single number
val format = LocalTime.Format {
    hour(); minute(); second()
    optional { char('.'); secondFraction(1, 9) }
}
val formatted = format.format(LocalTime(9, 34, 58, 120_000_000))
check(formatted == "093458.12") 
   //sampleEnd
}

open fun secondFraction(fixedLength: Int)(source)

The fractional part of the second without the leading dot.

When formatting, the decimal fraction will add trailing zeroes or be rounded as necessary to always output exactly the number of digits specified in fixedLength. Rounding is performed using the round-toward-zero rounding mode.

When parsing, exactly fixedLength digits will be consumed.

This field has the default value of 0. If you want to omit it, use optional.

See also the secondFraction overload that accepts two parameters, the minimum and maximum length of the fractional part.

See also

Throws

if fixedLength is not in the range 1..9.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Defining a custom format for the local time with a fixed-length second fraction
val format = LocalTime.Format {
    hour(); char(':'); minute(); char(':'); second()
    char('.'); secondFraction(fixedLength = 3)
}
val formatted = format.format(LocalTime(9, 34, 58, 120_000_000))
check(formatted == "09:34:58.120") 
   //sampleEnd
}