I have no idea why this isnt working

i have made a script to try to find Pi, but the text component i have referenced just says 4, btw i am using this method “Leibniz formula for π” - Leibniz formula for π - Wikipedia

Here is my code :

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PaddleShape : MonoBehaviour {

    private bool isNextNegative = false;
    private int lastFraction = 1;
    private decimal currentTotalOfFractions;
	private Text text;
	void Start()
		text = GetComponent<Text>();
		Invoke("Golden", 1.5f);
	void Golden()
        decimal fractionValue = 1 / lastFraction;

        if (isNextNegative)
            fractionValue = -fractionValue;

        currentTotalOfFractions += fractionValue;

        text.text = (currentTotalOfFractions * 4).ToString();

        lastFraction += 2;
        isNextNegative = !isNextNegative;
        Invoke("Golden", Mathf.Epsilon);

The part that looks suspicious here is

decimal fractionValue = 1 / lastFraction;

When you divide an integer by an integer, the result is always an integer.

If integer lastFraction is greater than 1, the result is 0.

I didn’t check the rest of the math but if you want to get a decimal result, make the “1” a “decimal 1” and the result will be 1 too.


decimal fractionValue = 1m / lastFraction;

“m” is the suffix that makes an integer literal decimal.