# Truncate decimal number

Can’t believe I can’t find this, but . . .

How do I truncate the decimal portion of a number?

I want to convert 10.5 to 10

You could use:

``Floor(10.5)``

There’s Round, Floor and Ceil for these purposes.
http://documentation.xojo.com/index.php/Round
http://documentation.xojo.com/index.php/Floor
http://documentation.xojo.com/index.php/Ceil

These work in iOS projects too, even though that’s not indicated by either the docs or auto-complete.

So, deep down the goal is to determine the format for a negative decimal number. -0.1 should be formated with #.0 and -0.15 should be shown with -#.00

I thought with formula would work, but it didn’t if X10 - floor(X10) = 0 then format with -#.0.

Using X = -0.10 gave me (-1) - (-2) = 1

Ceiling will likely do something similar. Round with definitely not work becuse -1.5 would be rounded to -1.0

Anyone have a better idea? . . . and why doesn’t Xojo have a truncate function?

For the new framework, all math-related methods are in the Math namespace.

Thanks Paul, but I’ve looked at all those functions. They don’t work. BTW . . . the last digit will always be zero or five: -0.05, -0.10 or -0.15, etc

Well, that seems unlikely. I’m not clear what you are trying to accomplish.

I see the results I expect:

``` Dim neg As Double = -0.10 Dim floor As Double = Xojo.Math.Floor(neg) // -1.0 Dim ceil As Double = Xojo.Math.Ceil(neg) // 0.0 Dim round As Double = Xojo.Math.Round(neg) // 0.0```

floor(-0.1) gives me -0.2 . . . Maybe it’s technically -0.10000001 so it goes to -0.2

But I figgered out a much simpler solution:

``````if right(format(X,"#.00;-#.00"),1) = "0" then
(X,"#.0;-#.0")
else
format(X,"#.00;-#.00")
end if``````

John, I can’t reproduce that. Floor( -0.1 ) gives me -1.0, as expected.

Same here. What platform? Post the code you 're using. Unless it’s a bug in the platform you’re using, there must be something else you’re doing that you’re not telling us about.

[quote=204792:@John Scanlan]Can’t believe I can’t find this, but . . .

How do I truncate the decimal portion of a number?

I want to convert 10.5 to 10[/quote]

dim d as double = 10.5
dim i as integer = d
done

If I understand the goal, it’s called “rounding towards zero”

``` v = if ( v<0 , ceil(v) , floor(v) ) ```

-1.5 gives you -1
1.5 gives you 1

Sounds to me like “figure out how many decimal places to use in a format statement” based on

[quote=204844:@John Scanlan]So, deep down the goal is to determine the format for a negative decimal number. -0.1 should be formated with #.0 and -0.15 should be shown with -#.00
[/quote]
The fun part here is of course that so many fractional values have no precise representation in binary so -0.1 may in fact just be -0.9999999999999 and -0.15 may be -0.1499999999 so there’s literally now way to know which rounding to use

I’d do something like what John finally landed on

[quote=204865:@John Scanlan]floor(-0.1) gives me -0.2 . . . Maybe it’s technically -0.10000001 so it goes to -0.2

But I figgered out a much simpler solution:

``````if right(format(X,"#.00;-#.00"),1) = "0" then
(X,"#.0;-#.0")
else
format(X,"#.00;-#.00")
end if[/quote]
``````

Always format it with “-0.##” and see if the last digit is 0 and if so redo it with “-0.#” to eventually drop the trailing 0

Or just trim the trailing zeros instead of calling format repeatedly.

dunno which would be quicker
either way “dropping the trailing 0” on it
get the right functionality then worry about optimizing

some get too far down the optimizing it first pathway then realize they have something really fast that does the wrong thing I do Tim’s technique:

```tempString = Format(myNumber, "###,###,###,##0.##") if right(tempString, 1) = "." then tempString = left(tempString , len(tempString) - 1) 'remove trailing period```

I did Norman’s method. Works fine.

if right(format(X,"#.00;-#.00"),1) = “0” then
format(X,"#.0;-#.0")
else
format(X,"#.00;-#.00")
end if