Skip to content
This repository was archived by the owner on Oct 26, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bintray.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
apply plugin: 'com.jfrog.bintray'
apply plugin: "maven-publish"


bintray {
user = project.hasProperty('bintrayUser') ? project.getProperty('bintrayUser') :
System.getenv('BINTRAY_USER') ?: ''
Expand Down
203 changes: 203 additions & 0 deletions src/main/kotlin/nectec/thai/unit/Length.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package nectec.thai.unit

import java.lang.StringBuilder
import java.math.BigDecimal
import java.text.NumberFormat


/**
* Thai length unit.
* Ref. https://en.wikipedia.org/wiki/Thai_units_of_measurement
* Created by max on 11/7/2560.
*/
data class Length (val centimetres: BigDecimal) {

constructor(centimetres: Number) : this(BigDecimal(centimetres.toDouble()))
constructor(centimetres: Double) : this(BigDecimal(centimetres))

constructor(yot: Number,sen:Number,wa:Number,sok:Number,khuep:Number,nio:Number,krabiat:Number) : this(toCentimetres(yot, sen, wa, sok, khuep, nio, krabiat))

val krabiat: Double
val nio: Int
val khuep: Int
val sok: Int
val wa: Int
val sen: Int
val yot: Int

val rounding_number_format :NumberFormat

init {

//Number rounding format.
rounding_number_format = NumberFormat.getNumberInstance()
rounding_number_format.maximumFractionDigits=0
rounding_number_format.roundingMode=java.math.RoundingMode.HALF_UP

var temp_value :BigDecimal


this.yot=toYOT(centimetres).toInt()
temp_value=centimetres.remainder(BigDecimal(CENTIMETRE_PER_YOT))

this.sen = toSEN(temp_value).toInt()
temp_value = temp_value.remainder(BigDecimal(CENTIMETRE_PER_SEN))

this.wa = toWA(temp_value).toInt()
temp_value = temp_value.remainder(BigDecimal(CENTIMETRE_PER_WA))

this.sok = toSOK(temp_value).toInt()
temp_value = temp_value.remainder(BigDecimal(CENTIMETRE_PER_SOK))

this.khuep = toKHUEP(temp_value).toInt()
temp_value = temp_value.remainder(BigDecimal(CENTIMETRE_PER_KHUEP))

this.nio = toNIO(temp_value).toInt()
temp_value = temp_value.remainder(BigDecimal(CENTIMETRE_PER_NIO))

this.krabiat = toKRABIAT(temp_value).toDouble()

}

companion object {

//Ref. https://en.wikipedia.org/wiki/Thai_units_of_measurement
@JvmField val CENTIMETRE_PER_KRABIAT = 0.5208
@JvmField val CENTIMETRE_PER_NIO = 2.083
@JvmField val CENTIMETRE_PER_KHUEP = 25
@JvmField val CENTIMETRE_PER_SOK = 50
@JvmField val CENTIMETRE_PER_WA = 200
@JvmField val CENTIMETRE_PER_SEN = 4000
@JvmField val CENTIMETRE_PER_YOT = 1600000

@JvmField val KRABIAT = " กระเบียด "
@JvmField val NIO = " นิ้ว "
@JvmField val KHUEP = " คืบ "
@JvmField val SOK = " ศอก "
@JvmField val WA = " วา "
@JvmField val SEN = " เส้น "
@JvmField val YOT = " โยชน์ "

private fun toCentimetres(yot: Number,sen:Number,wa:Number,sok:Number,khuep:Number,nio:Number,krabiat:Number):BigDecimal{
var temp_value : BigDecimal
temp_value= BigDecimal.ZERO
temp_value = temp_value.add(BigDecimal(yot.toDouble()).multiply(BigDecimal(CENTIMETRE_PER_YOT)))
temp_value = temp_value.add(BigDecimal(sen.toDouble()).multiply(BigDecimal(CENTIMETRE_PER_SEN)))
temp_value = temp_value.add(BigDecimal(wa.toDouble()).multiply(BigDecimal(CENTIMETRE_PER_WA)))
temp_value = temp_value.add(BigDecimal(sok.toDouble()).multiply(BigDecimal(CENTIMETRE_PER_SOK)))
temp_value = temp_value.add(BigDecimal(khuep.toDouble()).multiply(BigDecimal(CENTIMETRE_PER_KHUEP)))
temp_value = temp_value.add(BigDecimal(nio.toDouble()).multiply(BigDecimal(CENTIMETRE_PER_NIO)))
temp_value = temp_value.add(BigDecimal(krabiat.toDouble()).multiply(BigDecimal(CENTIMETRE_PER_KRABIAT)))

return temp_value
}
}

/**
* Print All
* Exam create object input Length(10,0,0,0,1,2,1)
* formalPrintAll output = 10 โยชน์ 0 เส้น 0 วา 0 ศอก 1 คืบ 2 นิ้ว 1 กระเบียด
*/
fun formalPrintAll(): String {
val stringBuilder = StringBuilder()
return stringBuilder
.append(yot).append(YOT)
.append(sen).append(SEN)
.append(wa).append(WA)
.append(sok).append(SOK)
.append(khuep).append(KHUEP)
.append(nio).append(NIO)
.append(rounding_number_format.format(krabiat)).append(KRABIAT)
.toString().trim()
}

/**
* Zero value no print.
* Exam create object input Length(10,0,0,0,1,2,1)
* formalPrint output = 10 โยชน์ 1 คืบ 2 นิ้ว 1 กระเบียด
*/
fun formalPrint(): String {
val stringBuilder = StringBuilder()
return stringBuilder
.append(if (yot>0){yot.toString()+YOT}else{""} )
.append(if (sen>0){sen.toString()+SEN}else{""} )
.append(if (wa>0){wa.toString()+WA}else{""} )
.append(if (sok>0){sok.toString()+SOK}else{""} )
.append(if (khuep>0){khuep.toString()+KHUEP}else{""} )
.append(if (nio>0){nio.toString()+NIO}else{""} )
.append(if (krabiat>0){rounding_number_format.format(krabiat)+KRABIAT}else{""} )
.toString().trim()
}


fun toYOT(centimetres: Double): Double {
return centimetres / CENTIMETRE_PER_YOT
}
fun toYOT(centimetres: BigDecimal): BigDecimal {
return BigDecimal(toYOT(centimetres.toDouble()))
}
fun toYOT(): Double {
return toYOT(centimetres.toDouble())
}

fun toSEN(centimetres: Double): Double {
return centimetres / CENTIMETRE_PER_SEN
}
fun toSEN(centimetres: BigDecimal): BigDecimal {
return BigDecimal(toSEN(centimetres.toDouble()))
}
fun toSEN(): Double {
return toSEN(centimetres.toDouble())
}

fun toWA(centimetres: Double): Double {
return centimetres / CENTIMETRE_PER_WA
}
fun toWA(centimetres: BigDecimal): BigDecimal {
return BigDecimal(toWA(centimetres.toDouble()))
}
fun toWA(): Double {
return toWA(centimetres.toDouble())
}

fun toSOK(centimetres: Double): Double {
return centimetres / CENTIMETRE_PER_SOK
}
fun toSOK(centimetres: BigDecimal): BigDecimal {
return BigDecimal(toSOK(centimetres.toDouble()))
}
fun toSOK(): Double {
return toSOK(centimetres.toDouble())
}

fun toKHUEP(centimetres: Double): Double {
return centimetres / CENTIMETRE_PER_KHUEP
}
fun toKHUEP(centimetres: BigDecimal): BigDecimal {
return BigDecimal(toKHUEP(centimetres.toDouble()))
}
fun toKHUEP(): Double {
return toKHUEP(centimetres.toDouble())
}

fun toNIO(centimetres: Double): Double {
return centimetres / CENTIMETRE_PER_NIO
}
fun toNIO(centimetres: BigDecimal): BigDecimal {
return BigDecimal(toNIO(centimetres.toDouble()))
}
fun toNIO(): Double {
return toNIO(centimetres.toDouble())
}

fun toKRABIAT(centimetres: Double): Double {
return centimetres / CENTIMETRE_PER_KRABIAT
}
fun toKRABIAT(centimetres: BigDecimal): BigDecimal {
return BigDecimal(toKRABIAT(centimetres.toDouble()))
}
fun toKRABIAT(): Double {
return toKRABIAT(centimetres.toDouble())
}

}
157 changes: 157 additions & 0 deletions src/main/kotlin/nectec/thai/unit/Weight.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package nectec.thai.unit

import java.math.BigDecimal
import java.text.NumberFormat

data class Weight (val gram :BigDecimal){

constructor(gram: Number) : this(BigDecimal(gram.toDouble()))
constructor(gram: Double) : this(BigDecimal(gram))
constructor(hap: Number,chang: Number,tamlueng: Number,baht: Number,salueng: Number) : this(toGram(hap, chang, tamlueng, baht, salueng))


val salueng: Double
val baht: Int
val tamlueng: Int
val chang: Int
val hap: Int

val rounding_number_format : NumberFormat

init {

//Number rounding format.
rounding_number_format = NumberFormat.getNumberInstance()
rounding_number_format.maximumFractionDigits=0
rounding_number_format.roundingMode=java.math.RoundingMode.HALF_UP

var temp_value :BigDecimal

this.hap=toHAP(gram).toInt()
temp_value=gram.remainder(BigDecimal(GRAM_PER_HAP))

this.chang = toCHANG(temp_value).toInt()
temp_value = temp_value.remainder(BigDecimal(GRAM_PER_CHANG))
this.tamlueng = toTAMLUENG(temp_value).toInt()
temp_value = temp_value.remainder(BigDecimal(GRAM_PER_TAMLUENG))
this.baht = toBAHT(temp_value).toInt()
temp_value = temp_value.remainder(BigDecimal(GRAM_PER_BAHT))
this.salueng = toSALUENG(temp_value).toDouble()


}

companion object {

@JvmField val GRAM_PER_SALUENG = 3.75
@JvmField val GRAM_PER_BAHT = 15
@JvmField val GRAM_PER_TAMLUENG = 60
@JvmField val GRAM_PER_CHANG = 1200
@JvmField val GRAM_PER_HAP = 60000

@JvmField val SALUENG = " สลึง "
@JvmField val BAHT = " บาท "
@JvmField val TAMLUENG = " ตำลึง "
@JvmField val CHANG = " ชั่ง "
@JvmField val HAP = " หาบ "

private fun toGram( hap: Number,chang: Number,tamlueng: Number,baht: Number,salueng: Number): BigDecimal{

var temp_value : BigDecimal
temp_value= BigDecimal.ZERO

temp_value = temp_value.add(BigDecimal(salueng.toDouble()).multiply(BigDecimal(GRAM_PER_SALUENG)))
temp_value = temp_value.add(BigDecimal(baht.toDouble()).multiply(BigDecimal(GRAM_PER_BAHT)))
temp_value = temp_value.add(BigDecimal(tamlueng.toDouble()).multiply(BigDecimal(GRAM_PER_TAMLUENG)))
temp_value = temp_value.add(BigDecimal(chang.toDouble()).multiply(BigDecimal(GRAM_PER_CHANG)))
temp_value = temp_value.add(BigDecimal(hap.toDouble()).multiply(BigDecimal(GRAM_PER_HAP)))
return temp_value
}
}

/**
* Print All
* Exam create object input Weight(2,2,2,2,30000)
* formalPrintAll output = 3 หาบ 45 ชั่ง 17 ตำลึง 2 บาท 0 สลึง
*/
fun formalPrintAll(): String {
val stringBuilder = StringBuilder()
return stringBuilder
.append(hap).append(HAP)
.append(chang).append(CHANG)
.append(tamlueng).append(TAMLUENG)
.append(baht).append(BAHT)
.append(rounding_number_format.format(salueng)).append(SALUENG)
.toString().trim()
}

/**
* Zero value no print.
* Exam create object input Weight(2,2,2,2,30000)
* formalPrint output = 3 หาบ 45 ชั่ง 17 ตำลึง 2 บาท
*/
fun formalPrint(): String {
val stringBuilder = StringBuilder()
return stringBuilder
.append(if (hap>0){hap.toString()+HAP}else{""} )
.append(if (chang>0){chang.toString()+CHANG}else{""} )
.append(if (tamlueng>0){tamlueng.toString()+TAMLUENG}else{""} )
.append(if (baht>0){baht.toString()+BAHT}else{""} )
.append(if (salueng>0){rounding_number_format.format(salueng)+SALUENG}else{""} )
.toString().trim()
}


fun toSALUENG(gram: Double): Double {
return gram / GRAM_PER_SALUENG
}
fun toSALUENG(gram: BigDecimal): BigDecimal {
return BigDecimal(toSALUENG(gram.toDouble()))
}
fun toSALUENG(): Double {
return toSALUENG(gram.toDouble())
}

fun toBAHT(gram: Double): Double {
return gram / GRAM_PER_BAHT
}
fun toBAHT(gram: BigDecimal): BigDecimal {
return BigDecimal(toBAHT(gram.toDouble()))
}
fun toBAHT(): Double {
return toBAHT(gram.toDouble())
}

fun toTAMLUENG(gram: Double): Double {
return gram / GRAM_PER_TAMLUENG
}
fun toTAMLUENG(gram: BigDecimal): BigDecimal {
return BigDecimal(toTAMLUENG(gram.toDouble()))
}
fun toTAMLUENG(): Double {
return toTAMLUENG(gram.toDouble())
}

fun toCHANG(gram: Double): Double {
return gram / GRAM_PER_CHANG
}
fun toCHANG(gram: BigDecimal): BigDecimal {
return BigDecimal(toCHANG(gram.toDouble()))
}
fun toCHANG(): Double {
return toCHANG(gram.toDouble())
}

fun toHAP(gram: Double): Double {
return gram / GRAM_PER_HAP
}
fun toHAP(gram: BigDecimal): BigDecimal {
return BigDecimal(toHAP(gram.toDouble()))
}
fun toHAP(): Double {
return toHAP(gram.toDouble())
}

}


20 changes: 20 additions & 0 deletions src/test/kotlin/nectec/thai/unit/LenghtJavaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package nectec.thai.unit;

/**
* Created by user on 11/7/2560.
*/

import org.junit.Assert;
import org.junit.Test;

import java.math.BigDecimal;

public class LenghtJavaTest {
@Test
public void name() throws Exception {
//Length length = new Length(new BigDecimal(50)).copy(new BigDecimal(100)).copy(new BigDecimal(50));
Length length = new Length(50);
Assert.assertEquals(1,length.getSok());
}

}
Loading