I am struggling to do somehting so simple in Crystal Reports

I have some numbers I.E
2.851
3.481
0.791

I want to round them up so they are
2.80
3.50
0.80

Recommended Answers

All 3 Replies

Using Math can help you. Also your "2.80" is wrong it will be "2.90". Try this

 Dim core As Double = 3.481
 REM: You can get or loop to your values using a loop instead of
 ' using just one example as I've done.
        Dim RoundAwayFromZero As Double = Math.Round(core, 1, MidpointRounding.AwayFromZero)
        Label1.Text = "The original value is: " & core & " The rounded value is: " & RoundAwayFromZero

I used a Label in my case.

If you are rounding up then your results should be 2.90, 3.50, and 0.80. IE, the first term is incorrect. There are standard algorithms for this which you can probably find in Wikipedia, such as this page: https://en.wikipedia.org/wiki/Rounding

Keep in mind that Math.Round exhibits (in my opinion) odd (no pun intended) behaviour. It behaves differently depending on whether the integer portion of a number is odd or even. For example

Math.Round(2.5) evaluates to 2.0
Math.Round(3.5) evaluates to 4.0

That's why I prefer to roll my own that always rounds x.5 up to the next integer. You can code this for any decimal place by multiplying by a factor of 10 and adding 0.5 before truncating. For example, to round 2.56 to one decimal

Multiply by 10 (to give 25.6)
Add 0.5 (to give 26.1)
Truncate (to give 26.0)
Divide by 10 (to give 2.6)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.