Function does not work fine for Negative Values passed to it

47 views
Skip to first unread message

Coder Q

unread,
Jun 11, 2021, 4:31:32 AM6/11/21
to Kdb+ Personal Developers
Function Name: FormatNumber[col;DecimalPlaces]
Parameters Passed [enlist 0.119;15] / til 15 decimal places
output will be "0.119"
but when negative values are passed to it should return the same  result after calculating the decimalPlaces
FormatNumber[enlist -0.119;15]
Output which im getting is "-1.881"

FormatNumber:{[col;DecimalPlaces] Rows:count col; $[DecimalPlaces<=4;afterDecimal:1_`string[floor "j"$(10 xexp DecimalPlaces)*col-floor col)%(10 xexp DecimalPlaces)];[fraction:col-floor col; c:where string[col-floor col] like "*e*"; col[IndexWithNonZeroDigitAfterFouthDecimalPlace]:col[IndexWithNonZeroDigitAfterFouthDecimalPlace]-floor col[IndexWithNonZeroDigitAfterFouthDecimalPlace]; afterDecimal:2_`string[(floor "j"$(10 xexp DecimalPlaces)*fraction)%(10 xexp DecimalPlaces) ]; afterDecimal[IndexWithNonZeroDigitAfterFouthDecimalPlace]:"0",/:1_afterDecimal[IndexWithNonZeroDigitAfterFouthDecimalPlace]; ]; PrecisionERR:0.000000000001; Result:({:reverse "," sv 3 cut reverse string[x] each floor col+PrecisionERR),`".",/: ?[0=count each DecimalPoint:Rows#enlist[DecimalPlaces#"0"]; afterDecimal]; :?[DecimalPlaces>0;Result;ssr[;".";""]each Result] };



thanks in advance
Reply all
Reply to author
Forward
0 new messages