• 0

[VB.NET]Truncate without rounding


Question

So lets say I have a number, 16.95. I want it truncated to one decimal place, but NO ROUNDING. I want 16.9 for the final result.

So far I'm using:

Dim first As Int32 = Math.Floor(tp)
Dim second As Int32 = Int((tp - first) * 10)
Dim TPs As String = first & "." & second

It works for the following example, but not every time. I think only when there's already one decimal place. Another example:

tp = 16.7

first = 16

tp - first = 6.9999885559082 <---- do not understand

TP is a single, if that matters. I'm having a problem getting that one digit in the tenths place.

Link to comment
https://www.neowin.net/forum/topic/643336-vbnettruncate-without-rounding/
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Dim tp As Single = 16.95

'Fix simply truncates the decimal whereas Math.Floor always rounds down
'  (e.g. Math.Floor(-16.95) = -17, but Fix(-16.95) = -16)
Dim first As Int32 = Fix(tp)
Dim second As Int32 = Int((tp - first) * 10)
Dim TPs As String = first &amp; "." &amp; second

tp = 16.95

first = 16

second = Int((16.95 - 16) * 10) = Int((.95) * 10) = Int(9.5) = 9

TPs = 16.9

For the record, it could be floating-point rounding errors. You might try using a Double rather than a Single.

Fix truncates. Int rounds down to the nearest integer less than or equal to itself, same as Math.floor IIRC. Other things like CInt and Round all use Banker's Rounding, which is a bit odd for some people... If you have a fractional part of .5 or less, the result rounds toward the nearest even integer. Otherwise, the result is rounded to the nearest integer.

Examples:

Using CInt on a value in the range -0.5 to 0.5 results in 0.

Using CInt on a value in the range -1.499999... to -0.500...001 or 0.500...001 to 1.499999... results in -1 or 1, depending on the sign.

Using CInt on a value in the range -2.5 to -1.5 or 1.5 to 2.5 results in -2 or 2, depending on the sign.

Using CInt on a value in the range -3.499999... to -2.500...001 or 2.500...001 to 3.499999... results in -3 or 3, depending on the sign.

Simply put, Banker's Rounding relies on that extra minute fractional piece to round to the appropriate integer. This is why I use Fix for everything - I don't have any issues with negative v.s. positive values like I would with Int or Math.floor or Math.ceiling or whatever. I also don't have rounding issues like with Round and CInt. Everything is predictable, in other words. Of course, the built-in Math.Round does a wonderful job of rounding "the normal way" already, so I suppose my reinventing the wheel using Fix is rather pointless. ^_^

Edited by rpgfan
  • 0

Truncate to 1 decimal place, no rounding:

Dim decimalNumber As Double = 16.95
Dim truncatedNumber As Double = Math.Truncate(decimalNumber * 10) / 10

Should work. Math.Truncate() gets the integral part of the number (the bit before the decimal point). Therefore, if you multiply that by 10, then truncate, and then divide after, you'll get the one decimal place without any rounding.

  • 0
  GSDragoon said:
So lets say I have a number, 16.95. I want it truncated to one decimal place, but NO ROUNDING. I want 16.9 for the final result.

tp = 16.7

first = 16

tp - first = 6.9999885559082 <---- do not understand

Single and double precision numbers are inherently approximative. It's very dangerous with such numbers, for instance, to compare 7 with 7 for equality: one could be 6.999999999 and the other 7.00000000001.

If you need exactitude, use integers only. Define a type that holds the number as an integer and a power of 10. But in most cases you should be fine with the built-in floats, doubles and decimals.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Let me guess, just big hyped cut scenes for what is really just more run around in the same old static urban zone machine gun vs. sniper game play.
    • I am not seeing a huge difference there.
    • This is what I came here to say even if just for a dedicated Windows PC gaming box. Gaming is the only use case have to use Windows these days.
    • The Outer Worlds 2 gets an October release date, reveals companion details by Pulasthi Ariyasinghe After Avowed, Obsidian Entertainment's next grand RPG was already revealed to be releasing in 2025, but during the Xbox Games Showcase today, it finally received a firm release date: October 29. Check out the new story trailer above. While the original was set in Halcyon, this time, the story is set in Arcadia, a brand-new setting that's described as a lawless frontier. Also cut off from Earth, this land is pressured by human conflicts as well as mysterious space-time rifts. "You’ve been sent in as an Earth Directorate agent, but how you handle the mission – who you help, hinder, or exploit – is entirely your call," says Obsidian today. "Build your character with expanded traits, flaws, and backgrounds that shape every decision and unlock new ways to fight, sneak, talk, or blow things up." The RPG will let players get involved with three clashing factions this time: The Protectorate, Auntie’s Choice, and The Order of the Ascendant. Each of them is attempting to advance their own version of humanity's optimal future. Each of these factions has tout own hub areas, followers, audio design, and propaganda elements, with the war between the trio changing all of these as the story progresses depending on player choices. "RPG systems have been reimagined for this sequel to give players more freedom and more flavor," adds the company. "For example, traits and flaws dynamically evolve based on how you play. Steal enough and you’ll be offered Kleptomaniac, which boosts loot sales but risks auto-theft when you so much as glance at an item." Obsidian also detailed the companions players will be able to meet and ally with in their journeys: Niles: Another Earth Directorate recruit torn between duty and defection. Inez: A former experiment from Auntie’s Choice with a grafted combat edge and a moral core. Aza: A chaos-loving Rift worshipper with a taste for violence and room to grow – maybe. Marisol: A stoic killer from the Order of the Ascendant with calculations to settle. Tristen: A walking tank and judge from the Protectorate, looking to dispense justice – or redefine it. Valerie: A floating, chirping support unit with unexpected upgrades and untapped potential. The Outer Worlds 2 is releasing on October 29, 2025 across PC, Xbox Series X|S, and PlayStation 5. As usual for an Xbox game, it will be available to Game Pass subscribers on day one for no extra cost. Don't forget that Obsidian also has Grounded 2 releasing next month into early access too.
    • I love space RPGs, and this one will no doubt scratch that itch. Im still modding the crap out of Starfiled. Can't wait.
  • Recent Achievements

    • Reacting Well
      BlakeBringer earned a badge
      Reacting Well
    • Reacting Well
      Lazy_Placeholder earned a badge
      Reacting Well
    • Dedicated
      Epaminombas earned a badge
      Dedicated
    • Veteran
      Yonah went up a rank
      Veteran
    • First Post
      viraltui earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      471
    2. 2
      +FloatingFatMan
      265
    3. 3
      ATLien_0
      235
    4. 4
      snowy owl
      224
    5. 5
      Edouard
      174
  • Tell a friend

    Love Neowin? Tell a friend!