0

I have a float, say 14.55e9 and I want to stringify it to "14550000000" the most nimic, clean and performant way as possible.

For now I only could find something based on slicing: $myFloat[0..$myFloat.find('.')]

The formatFloat method from strutils outputs "14550000000.0" if you ask for precision=0

v.oddou
  • 6,476
  • 3
  • 32
  • 63

1 Answers1

1

you can use

import json, math
var intvalue = int(math.floor( 14.55));
echo  %*(intvalue);

EDIT: Sorry i posted some javascript code. Math is lowercase in Nim and JSON.stringify was from javascript

zetawars
  • 1,023
  • 1
  • 12
  • 27
  • like this ? https://tio.run/##y8vM/f8/M7cgv6hEwSvY309HITexJIOrLLFIITOvpCwxpzRVwVbBFyiml5aTn1@koWBoomdqqmnNBVKtV1xSlJmXnplWqQFTjSmDpBmiV9P6/38A it doesn't build :( :( – v.oddou Feb 19 '18 at 07:23
  • yes, sorry it was an accident. use Math as lowercase. – zetawars Feb 19 '18 at 07:28
  • I did what I could to make it build, resulting in this: https://tio.run/##y8vM/f8/M7cgv6hEIas4P09HITexJENHobikqLQkM6eYqyyxSCEzr6QsMac0VcEWLKuXlpOfX6RhaKJnaqppzZWanJGvoALSrJeXWu7llpOfWKIB04JLHtkITev//wE you'll note that the output still is `14.0` and not `14` – v.oddou Feb 19 '18 at 07:33
  • aaah, ok but sorry, I forgot to specify that my integers will not fit the `int` type, so it has to be directly formatted from the float value, not going through an int cast, of course I already thought of that, it would be too easy :) (because the exponential range of float, it can go weeeell beyond 2 billion very quickly) – v.oddou Feb 19 '18 at 07:49