I want any amount ending in 98 cents.

Three examples:
5 euros x 1.25 euros then the result is 6.25 euros, the program change this to 6.95
4 euros + 2 euros then the result is 6.00 euros, the program change this to 6.95
User type to 6.70then the program change this to 6.95

How can I solve it very easily?

Thanks,

andre

Dani AI

Generated

The thread mixes two different goals. The title asks for amounts ending in 98 cents but the three examples all end in .95; should confirm which cents value is intended. ’s quick solution forces the fractional part to .95, which is fine if the intent is "always replace cents with .95" — but it will decrease values whose cents are greater than .95 (for example 6.99 becomes 6.95). If the real intent is "produce the smallest amount that is greater than or equal to the original and that ends with .95 (or .98)", use a round-up-to-target-cents approach instead.

A reliable VB.NET pattern (uses Decimal for money and rounds to cents first) is:

Dim desiredCents As Integer = 95   ' change to 98 if needed
Dim amount As Decimal = 6.25D

Dim cents As Integer = CInt(Math.Round(amount * 100D, 0, MidpointRounding.AwayFromZero))
Dim units As Integer = cents \ 100
Dim candidateCents As Integer = units * 100 + desiredCents

If candidateCents < cents Then
    candidateCents += 100
End If

Dim result As Decimal = candidateCents / 100D

This produces the smallest result >= original that ends with the specified cents. Use Decimal throughout to avoid binary-floating errors and round to two decimals before converting to integer cents; see the .NET Decimal type and Math.Round docs for details (Decimal struct) (Math.Round). Handle negative amounts explicitly if they can occur, and choose whether the logic should "always replace cents" (the simpler truncate+add-cents approach) or "round up to next matching cents" (the pattern above).

Use this pattern to all:

Dim a As Decimal = 6.25D
//pattern:
Dim fraction As Decimal = a - CInt(Math.Truncate(a))
a = a - fraction + (0.95D)
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.