Skip to content

Commit 0d1be7c

Browse files
authored
fix number formatting (#154)
1 parent 73d7013 commit 0d1be7c

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

shared/src/main/scala/org/adridadou/openlaw/parser/template/variableTypes/NumberType.scala

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ case object NumberType extends VariableType("Number") {
2020
if(constructorType =!= this) {
2121
Failure(s"the constructor type should be $name but is ${constructorType.name}")
2222
} else {
23-
attempt(expr.evaluateT[OpenlawBigDecimal](executionResult).map(OpenlawBigDecimal(_)))
23+
attempt(expr.evaluateT[OpenlawBigDecimal](executionResult).map(OpenlawBigDecimal))
2424
}
2525
case _ =>
2626
Failure(s"the constructor for $name only handles single values")
@@ -79,22 +79,25 @@ case object NumberType extends VariableType("Number") {
7979

8080
trait NumberFormatter {
8181
def formatNumber(bd:BigDecimal):String = {
82-
bd.bigDecimal.toPlainString.replaceAll("(\\d)(?=(\\d{3})+$)", "$1,")
82+
val regex = "(\\d)(?=(\\d{3})+$)"
83+
val fullNumber = bd.bigDecimal.toPlainString
84+
val split = fullNumber.split("\\.")
85+
val intNumber = split(0)
86+
val decimalNumber = split.lift(1)
87+
val formattedIntNumber = intNumber.replaceAll(regex, "$1,")
88+
89+
decimalNumber.map(d => formattedIntNumber + "." + d).getOrElse(formattedIntNumber)
8390
}
8491
}
8592

8693
case object NoTrailingZerosFormatter extends Formatter with NumberFormatter {
8794
override def format(value: OpenlawValue, executionResult: TemplateExecutionResult): Result[Seq[AgreementElement]] =
88-
attempt(VariableType.convert[OpenlawBigDecimal](value).bigDecimal.stripTrailingZeros()) map {
89-
case bd => Seq(FreeText(Text(formatNumber(bd))))
90-
}
95+
attempt(VariableType.convert[OpenlawBigDecimal](value).bigDecimal.stripTrailingZeros()) map (bd => Seq(FreeText(Text(formatNumber(bd)))))
9196
}
9297

9398
case object RawNumberFormatter extends Formatter with NumberFormatter {
9499
override def format(value: OpenlawValue, executionResult: TemplateExecutionResult): Result[Seq[AgreementElement]] =
95-
attempt(VariableType.convert[OpenlawBigDecimal](value).bigDecimal.stripTrailingZeros().toPlainString) map {
96-
case str => Seq(FreeText(Text(str)))
97-
}
100+
attempt(VariableType.convert[OpenlawBigDecimal](value).bigDecimal.stripTrailingZeros().toPlainString) map (str => Seq(FreeText(Text(str))))
98101
}
99102

100103
case class Rounding(expr:Expression) extends Formatter with NumberFormatter {

shared/src/test/scala/org/adridadou/openlaw/vm/OpenlawExecutionEngineSpec.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,4 +1372,27 @@ class OpenlawExecutionEngineSpec extends FlatSpec with Matchers with OptionValue
13721372

13731373
missingInputs shouldBe Seq(VariableName("Email"))
13741374
}
1375+
1376+
it should "format small numbers properly" in {
1377+
val text =
1378+
"""
1379+
[[my number:Number]]
1380+
""".stripMargin
1381+
1382+
val template = compile(text)
1383+
1384+
engine.execute(template, TemplateParameters("my number" -> "0.000000042")) match {
1385+
case Success(executionResult) =>
1386+
println(parser.forReview(executionResult.agreements.head))
1387+
parser.forReview(executionResult.agreements.head) shouldBe "<p class=\"no-section\"><br /> 0.000000042<br /> </p>"
1388+
case Failure(ex, message) => fail(message ,ex)
1389+
}
1390+
1391+
engine.execute(template, TemplateParameters("my number" -> "4200000000")) match {
1392+
case Success(executionResult) =>
1393+
println(parser.forReview(executionResult.agreements.head))
1394+
parser.forReview(executionResult.agreements.head) shouldBe "<p class=\"no-section\"><br /> 4,200,000,000<br /> </p>"
1395+
case Failure(ex, message) => fail(message ,ex)
1396+
}
1397+
}
13751398
}

0 commit comments

Comments
 (0)